DataBindings Help

  • Thread starter Thread starter Andrew Bassett
  • Start date Start date
A

Andrew Bassett

Hey everyone...
I have what I hope is a simple question. I have a form with about a dozen
controls bound to a databinding source. That datasource is bound to a
dataset. Without using a binding navigator, how do I do anything with this
thing?? I can't for the life of me figure out how to add, update, etc... Is
this possible without the navigator?

Thanks!!
 
Hello,

BindingSource component provide some method to add, update records, for
example, to add a record, call AddNew() method; to update, call EndEdit()
method; to remove a record, call remove() mthod. All these can be done
without a navigator.

Hope this help,

Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
When I try to make this work I run into a problem. I call addnew and then
should be able to call endedit. However, when I do that I keep getting an
error kicked back about null values for columns. This happens with or
without data in my columns. Why is it its not recognizing the values I put
in my text boxes?? Does this make sense? As an example...

If I have a text box called Key and I put something in it I get a null
reference error. But, if after calling AddNew I set the value of my textbox
via code then it recognizes that value.

It seems like maybe I broke something with my databinding but it half way
works ...
 
I think my problem was the lack of the CurrencyManager. I don't have one of
those anywhere ... Thanks for the quick reply, I'm off to explore.
 
On your example you have the following line....

cma = DirectCast(BindingContext(dt), CurrencyManager)

I'm having trouble making that work. If I try this...

cma = DirectCast(Me.Sites_MBindingSource, CurrencyManager)

I get an error stating I cannot convert a binding source to a Currency Manager. How can I make that work?
 
In my test, after call Addnew, the TextBox is changed to blank and I can
type some string in it. Then I click a button to call
bindingSource1.EndEdit() and this work.

Are all of the fileds in bindingSource bound to a control on the form?
Before EndEdit(), is each of these controls has a actual value? When you
get the exception like

Column 'abc' does not allow nulls.

You may check if the field 'abc' is bound to a control (for example, a
TextBox) and if it has a actual value


Luke Zhang
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
You can do same thing without Navigator by using –


'Moves data item First
Private Sub btnMoveFirst_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveFirst.Click
Me.BindingContext(DA, "Employee").Position = 0
End Sub
'Moves data item Last
Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveLast.Click
Me.BindingContext(DA, "Employee").Position = Me.BindingContext(DA,
"Employee").Count - 1
End Sub
'Moves data item Next
Private Sub btnMoveNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveNext.Click
Me.BindingContext(DA, "Employee").Position -= 1
End Sub
'Moves data item Prev
Private Sub btnMovePrev_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMovePrev.Click
Me.BindingContext(DA, "Employee").Position += 1
End Sub
'Add New item
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddNew.Click
Dim instance As BindingNavigator
Dim value As ToolStripItem
value = instance.AddNewItem
instance.AddNewItem = value
End Sub
'Delete item
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDel.Click
Dim instance As BindingNavigator
Dim value As ToolStripItem
value = instance.DeleteItem
instance.DeleteItem = value
End Sub
 
You can do the same without Navigator by using –
'Moves data item First
Private Sub btnMoveFirst_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveFirst.Click
Me.BindingContext(DA, "TBL").Position = 0
End Sub
'Moves data item Last
Private Sub btnMoveLast_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveLast.Click
Me.BindingContext(DA, "TBL").Position = _
Me.BindingContext(DA,"TBL").Count - 1
End Sub
'Moves data item Next
Private Sub btnMoveNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMoveNext.Click
Me.BindingContext(DA, "TBL").Position -= 1
End Sub
'Moves data item Prev
Private Sub btnMovePrev_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnMovePrev.Click
Me.BindingContext(DA, "Employee").Position += 1
End Sub
'Add New item
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddNew.Click
Dim instance As BindingNavigator
Dim value As ToolStripItem
value = instance.AddNewItem
instance.AddNewItem = value
End Sub
'Delete item
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDel.Click
Dim instance As BindingNavigator
Dim value As ToolStripItem
value = instance.DeleteItem
instance.DeleteItem = value
End Sub

As an alternative download IdeaBlade’s DevForce Express and use that for ORM (I don’t work for them – but use it extensively)
 
Back
Top