user1532468
user1532468

Reputation: 1751

Adding items to a datagrid [advice needed]

I am used to working with listview in vb.Net where to add items you just use as an example:

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dr("Box start").ToString())

However, I cannot find a way to add items or sub-items to a datagrid via intellisense. All the googling I have done just throws up DataGridView which is the newer control, but I am only interested in DataGrid. If anyone could offer any advice or tuts I would be grateful. many thanks

Upvotes: 0

Views: 101

Answers (1)

David
David

Reputation: 219037

You don't necessarily want to add items to a DataGridView, you want to add (or edit, remove, in some way make changes to) items to the underlying data source to which the DataGridView is bound.

For example, if you have a Person class and you bind the DataGridView to a List(Of Person), it might look something like this:

Dim personList As New List(Of Person)()

' elsewhere...
personList = GetPeople()
dataGridView1.DataSource = personList

At this point, you manage the elements in personList, not the DataGridView itself. So to add an element:

Dim newPerson As New Person()
' set some values on newPerson

personList.Add(newPerson)

You may need to "refresh" the DataGridView to reflect the changes:

dataGridView1.Refresh()

or:

dataGridView1.DataSource = Nothing
dataGridView1.DataSource = personList

Upvotes: 2

Related Questions