DataGridView binding problem

G

Guest

I'm trying to bind a DataGridView to a generic List of objects. My problem
is if I initially bind a DataGridView to a list with no entries, I can't seem
to get it to rebind to anything else successfully after that.

Below is a sample of the sort of thing I'm trying to do. The form has a
DataGridView and a button (btnAdd). If I uncomment the one line in
Form1_Load, this code works with no problems. With the line commented out I
can't ever seem to get it to work. Any insights on getting around this
behavior are appreciated.

Public Class Form1
Private _list As New TestList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
_list.Add(New Test)
Call BindList()
End Sub
Private Sub BindList()
DataGridView1.DataBindings.Clear()
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = _list
DataGridView1.AutoResizeColumns()
If DataGridView1.RowCount > 0 Then
DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.RowCount - 1)
End If
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
_list.Add(New Test)
Call BindList()
End Sub
End Class
Public Class Test
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
Public Class TestList
Inherits List(Of Test)
End Class
 
G

Guest

The generic work around that seems to work ok is:
If _list.Count = 0 Then
_list.Add(New Test)
Call BindList()
_list.RemoveAt(0)
End If
Call BindList()

If anyone knows the correct way to do this, I'd love to see it though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top