Solved

Databound DataGridView not refreshing correctly

Posted on 2011-09-08
4
481 Views
Last Modified: 2012-05-12
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:
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

Open in new window


I didn't include the designer code for brevity.

Thanks,
Lance
0
Comment
Question by:melance42
  • 2
  • 2
4 Comments
 
LVL 41

Assisted Solution

by:graye
graye earned 500 total points
ID: 36511151
You need to add the WithEvents key word when defining your BindingList.  For example:

Private WithEvents _data As BindingList(Of Test)

http://msdn.microsoft.com/en-us/library/aty3352y(v=VS.90).aspx
0
 

Author Comment

by:melance42
ID: 36511493
Graye,
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

You need to add the WithEvents key word when defining your BindingList.  For example:

Private WithEvents _data As BindingList(Of Test)

http://msdn.microsoft.com/en-us/library/aty3352y(v=VS.90).aspx
0
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
ID: 36511617
There's a pretty good example at the bottom of this page.  It shows how to configure the bindinglist to allow add/remove and to raise events upon changes

http://msdn.microsoft.com/en-us/library/ms132679.aspx
0
 

Author Comment

by:melance42
ID: 36512337
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net 1 month apart 11 39
Optimize the query 5 50
How to limit User Input 2 33
Broken .resx file generating errors 18 15
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question