Results 1 to 10 of 10

Thread: [RESOLVED] Binding Confusion

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Resolved [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...

    Quote 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:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.  
    3.         'create the bound table. This will come from mySQL server
    4.         Dim dt As New DataTable
    5.         dt.Columns.AddRange({New DataColumn("whatToDo")})
    6.         dt.Rows.Add("have")
    7.         dt.Rows.Add(" a ")
    8.         dt.Rows.Add("beer")
    9.         dt.Rows.Add(" ;) ")
    10.         dt.AcceptChanges()
    11.  
    12.  
    13.         'set the bindings....
    14.         BindingSource1.DataSource = dt
    15.         Me.BindingNavigator1.BindingSource = BindingSource1
    16.         Me.TextBox1.DataBindings.Add("Text", BindingSource1, "whatToDo")
    17.  
    18.  
    19.  
    20.  
    21.     End Sub
    22.  
    23.  
    24.     Private Sub BindingNavigatorMoveNextItem_Click(sender As Object, e As EventArgs) Handles    BindingNavigatorMoveNextItem.Click
    25.  
    26.  
    27.         Me.Validate()
    28.  
    29.  
    30.         Dim row As DataRow = DirectCast(BindingSource1.Current, DataRowView).Row
    31. Dim value As String = row("whatToDo").ToString
    32.         Dim rowState = row.RowState
    33.  
    34.  
    35.  
    36.  
    37.         Debug.Print(String.Format("{0}     {1}", value, rowState))
    38.  
    39.  
    40.  
    41.  
    42.     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

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,515

    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?

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Binding Confusion

    I'm guessing that you could handle the RowChanged event of the underlying DataTable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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

  6. #6

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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

  7. #7

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    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

  8. #8
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: [RESOLVED] Binding Confusion

    Quote Originally Posted by kebo View Post
    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.

  9. #9

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: [RESOLVED] Binding Confusion

    Quote Originally Posted by TnTinMN View Post
    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

  10. #10
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    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:
    1. Dim drv As DataRowView = DirectCast(BindingSource1.Current, DataRowView)
    2. Dim row As DataRow = drv.Row
    3.  
    4. If drv.IsEdit Then
    5.     drv.EndEdit()
    6. End If
    7.  
    8. Dim value As String = row("whatToDo").ToString
    9. Dim rowState As DataRowState = row.RowState
    10. 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
  •  



Click Here to Expand Forum to Full Width