Hey all,
When you bind a DataGridView to a DataTable it is easy to programmatically add rows - by just adding to the bound DataSource (which in this case is a DataTable).
Ive been binding typed lists of objects to my DataGridView. This works fine, but I am unable to display new rows in the DataGridView when I add additional objects to the bound list..
Any ideas? Thanks for any ideas/explanation. Im guessing this just cant be done..
mafro
The code that follows is a working example.
When you bind a DataGridView to a DataTable it is easy to programmatically add rows - by just adding to the bound DataSource (which in this case is a DataTable).
Ive been binding typed lists of objects to my DataGridView. This works fine, but I am unable to display new rows in the DataGridView when I add additional objects to the bound list..
Any ideas? Thanks for any ideas/explanation. Im guessing this just cant be done..
mafro
The code that follows is a working example.
VB.NET:
[COLOR="Green"]'in my form[/COLOR]
[COLOR="Blue"]Dim [/COLOR]items [COLOR="Blue"]As New[/COLOR] List([COLOR="Blue"]Of [/COLOR]DemoObj)
items.Add([COLOR="Blue"]New[/COLOR] DemoObj(1, "some data"))
dgv.DataSource = items
[COLOR="Green"]'the object being bound[/COLOR]
[COLOR="Blue"]Class [/COLOR]DemoObj
[COLOR="Blue"]Private [/COLOR]_pk [COLOR="Blue"]As [/COLOR]Int32
[COLOR="Blue"]Private [/COLOR]_data [COLOR="Blue"]As String[/COLOR]
[COLOR="Blue"]Public Property[/COLOR] pk [COLOR="Blue"]As Int32[/COLOR]
[COLOR="Blue"]Get[/COLOR]
[COLOR="Blue"]Return [/COLOR]_pk
[COLOR="Blue"]End Get
Set[/COLOR]([COLOR="Blue"]ByVal [/COLOR]value [COLOR="Blue"]As Int32[/COLOR])
_pk = value
[COLOR="Blue"]End Set
End Property[/COLOR]
[COLOR="Blue"]Public Property[/COLOR] data [COLOR="Blue"]As String[/COLOR]
[COLOR="Blue"]Get[/COLOR]
[COLOR="Blue"]Return [/COLOR]_data
[COLOR="Blue"]End Get
Set[/COLOR]([COLOR="Blue"]ByVal [/COLOR]value [COLOR="Blue"]As String[/COLOR])
_data = value
[COLOR="Blue"]End Set
End Property[/COLOR]
[COLOR="Blue"]Public Sub New[/COLOR](pk [COLOR="Blue"]As Int32[/COLOR], data [COLOR="Blue"]As String[/COLOR])
_pk = pk
_data = data
[COLOR="Blue"]End Sub
End Class[/COLOR]