melance42
asked on
Databound DataGridView not refreshing correctly
I have a form that contains a datagridview with it's datasource set to a bindinglist(of T) and several controls. When a row is selected in the datagridview, I bind that row's DataBoundItem properties to the controls. When I need a new record, the user clicks an "Add New" button which in turn adds a record the BindingList(of T). All of this works and I see the new row in the datagridview, however; when I edit the values using the controls, the datagridview only reflects the first value I change. If I then change rows in the datagridview or highlight the current row, the values update. How can I get the datagridview to keep in sync with the underlying datasource?
A short example:
I didn't include the designer code for brevity.
Thanks,
Lance
A short example:
Imports System.ComponentModel
Public Class frmMisc
Private Class Test
Public Sub New()
End Sub
Public Sub New(ByVal one As String,
ByVal two As String)
Me.One = one
Me.Two = two
End Sub
Public Property One
Public Property Two
End Class
Private _data As BindingList(Of Test)
Private Sub Misc_Load(sender As Object, e As System.EventArgs) Handles Me.Load
_data = New BindingList(Of Test)
_data.Add(New Test("1", "2"))
DataGridView1.DataSource = _data
End Sub
Private Sub DataGridView1_RowEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
Dim row As Test = DataGridView1.Rows(e.RowIndex).DataBoundItem
If row IsNot Nothing Then
TextBox1.DataBindings.Clear()
TextBox1.DataBindings.Add("Text", row, "One")
TextBox2.DataBindings.Clear()
TextBox2.DataBindings.Add("Text", row, "Two")
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
_data.Add(New Test)
End Sub
End Class
I didn't include the designer code for brevity.
Thanks,
Lance
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Graye,
Thanks for the link. It still isn't acting in the exact manner that I would like, however; if I handle the validated event of my detail controls and call the ResetBindings method things seem to work well enough. I just hate to have to handle all of those events.
Thanks,
Lance
Thanks for the link. It still isn't acting in the exact manner that I would like, however; if I handle the validated event of my detail controls and call the ResetBindings method things seem to work well enough. I just hate to have to handle all of those events.
Thanks,
Lance
ASKER
I added the WithEvents key word and it I still don't see the data grid view syncing with the object. Am I going to have to force a data refresh in the data grid view inside of the BindingList events? It seems there should be a way to do this without having to force the binding to update manually.
Thanks,
Lance