|
-
Nov 2nd, 2016, 05:14 PM
#1
[RESOLVED] Binding Confusion
Hi all,
I have the following code on a form. It uses a Binding Navigator, Binding Source and a Text Box. In the load event, I create a table and set bindings. Everything binds correctly, and the Text Box text changes when I use the navigator.
In the navigator's MoveNext button click handler I fetch the binding source's current row and display the row's value which is bound to the text box. I also output the row's state.
Immediately after the form loads, the text box says "have". If I change it to "must have" and then navigate to the next record, this is what is output to the debug window...
 Originally Posted by Console Window
must have Unchanged
So the row value is now "Must have", but it was "have" so it has changed yet the row state is unchanged. If I then navigate back to the first record then navigate forward again, the row state finally changes.
So if the row value is different, why is it not reflected in the rowState?
The code; add a binding navigator, binding source and a text box to a form and drop it in...
vb.net Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'create the bound table. This will come from mySQL server Dim dt As New DataTable dt.Columns.AddRange({New DataColumn("whatToDo")}) dt.Rows.Add("have") dt.Rows.Add(" a ") dt.Rows.Add("beer") dt.Rows.Add(" ;) ") dt.AcceptChanges() 'set the bindings.... BindingSource1.DataSource = dt Me.BindingNavigator1.BindingSource = BindingSource1 Me.TextBox1.DataBindings.Add("Text", BindingSource1, "whatToDo") End Sub Private Sub BindingNavigatorMoveNextItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorMoveNextItem.Click Me.Validate() Dim row As DataRow = DirectCast(BindingSource1.Current, DataRowView).Row Dim value As String = row("whatToDo").ToString Dim rowState = row.RowState Debug.Print(String.Format("{0} {1}", value, rowState)) End Sub
The whole reason for this is so that I can update the data base if and when a record has changed rather than after the form is closed. If there is a better way, please let me know.
thanks for looking
Kevin
Last edited by kebo; Nov 2nd, 2016 at 05:47 PM.
Reason: removed superfluous code
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Nov 2nd, 2016, 05:33 PM
#2
Re: Binding Confusion
I've done this before but I use the BindingSource Position Change event. The question I've got is,
Code:
Dim rowState = row.RowState
Giving you the row state value of the row you just moved to or the row you were just on?
-
Nov 2nd, 2016, 05:46 PM
#3
Re: Binding Confusion
It's data from the row I am moving from (the one with the new value) since I am getting only one row and showing the new value and state from it.
Last edited by kebo; Nov 2nd, 2016 at 07:14 PM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Nov 2nd, 2016, 08:16 PM
#4
Re: Binding Confusion
I'm guessing that you could handle the RowChanged event of the underlying DataTable.
-
Nov 2nd, 2016, 08:19 PM
#5
Re: Binding Confusion
You can use the BindingSource.ListChanged event with appropriate filter.
Code:
Private Sub BindingSource1_ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs) Handles BindingSource1.ListChanged
If e.ListChangedType = System.ComponentModel.ListChangedType.ItemChanged Then
Dim row As DataRow = DirectCast(BindingSource1.List.Item(e.OldIndex), DataRowView).Row
Dim value As String = row("whatToDo").ToString
Dim rowState As DataRowState = row.RowState
Debug.Print(String.Format("{0} {1}", value, rowState))
End If
End Sub
-
Nov 2nd, 2016, 08:25 PM
#6
Re: Binding Confusion
I'm guessing that you could handle the RowChanged event of the underlying DataTable.
That would be one hellava good guess. It certainly does what I need.
thanks
props
kevin
edit...
@tnt
Took me a little bit to read that. I think that would be better since I won't need to hook the table events.
thanks
Last edited by kebo; Nov 2nd, 2016 at 08:32 PM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Nov 2nd, 2016, 08:36 PM
#7
Re: [RESOLVED] Binding Confusion
I've marked this resolved, but I'm still curious if anyone can explain why I get a row from the binding source with changed data yet has a row state that is unchanged.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Nov 2nd, 2016, 08:44 PM
#8
Re: [RESOLVED] Binding Confusion
 Originally Posted by kebo
I've marked this resolved, but I'm still curious if anyone can explain why I get a row from the binding source with changed data yet has a row state that is unchanged.
Timing is everything. 
Nothing has changed in the BindingSource at the point you checked it. The Nav Button Click event is sent before the position is updated.
Edit:
You could force it by calling BindingSource.EndEdit.
-
Nov 2nd, 2016, 09:00 PM
#9
Re: [RESOLVED] Binding Confusion
 Originally Posted by TnTinMN
Timing is everything.
Nothing has changed in the BindingSource at the point you checked it.
I get a DataViewRow from the BindingSource, and from it I get the DataRow from the bound table. Using a break point, I can see that data in that row has changed, yet the RowState say it's unchanged. It's either changed or it's not yea?
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Nov 2nd, 2016, 09:33 PM
#10
Re: [RESOLVED] Binding Confusion
I'm guessing that you believe that the call to "Me.Validate" should be pushing the value to the DataSource, but it doesn't.
The BindingSource is bound to the DataTable through the DataTable.DefaultView (a DataView). This is why the BindingSource.Items are type DataRowView. The DataRowView is in an edit mode after you change the bound TextBox.Text. At this point no changes have been pushed back to the DataTable.
You can check the edit state of the DataRowView via its IsEdit property.
VB.Net Code:
Dim drv As DataRowView = DirectCast(BindingSource1.Current, DataRowView) Dim row As DataRow = drv.Row If drv.IsEdit Then drv.EndEdit() End If Dim value As String = row("whatToDo").ToString Dim rowState As DataRowState = row.RowState Debug.Print(String.Format("{0} {1}", value, rowState))
If the above, EndEdit is called directly on the DataRowView. You could also force it with BindingSource.EndEdit or BindingSource.CurrencyManager.EndCurrentEdit. Pick your poison; or in this case, the cure to your currency dilemma.
You have your code in a method that executes before the BindingSource is kicked in the butt (change position) to do something. After the click handler is finished, the BindingNavigator tells the BindingSource to change the index of its Current item. At that point it ends editing on the previous item (the DataRowView) and the changes are pushed to the DataSource.
If this is still clear as mud, then you'll have to wait for someone else to explain. That's the best I can do.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|