Question Updating a DataGridView to a Collection

duckkiller53

Member
Joined
May 11, 2009
Messages
5
Programming Experience
3-5
Could someone help:

I have a datagridview that I am binding to a collection. What I need to do is process each object in the collection (send quotes to our customers), and then display the items status in the grid. Then once the collection is completely processed clear the grid of items for the next round. I am binding to the collection with .DataSource = collection.

My Question:
What would be the best way to update the grid as the collection is processed one object at a time.

Initially I had thought about storing each item in a database table and then bind the grid to the table. Then as I processed each record, update the record and rebind the grid. The problem I had with that method was that I would be constantly writing and deleting data from the same table. That seemed unnecessary vs just using a collection and updating the collection.


Any suggestions would be greatly appreciated.
 
What type is your collection exactly? I'm guessing that it's a generic List. You should use a BindingList instead, which you can create like this:
VB.NET:
Dim myList As New List(Of SomeType)

'Populate myList here.

Dim myBindingList As New System.ComponentModel.BindingList(Of SomeType)(myList)
Or you can create the BindingList from scratch and add the items to it directly.

You can then process your items like this:
VB.NET:
Dim item As SomeType

For index As Integer = 0 To myBindingList.Count - 1
    item = myBindingList(index)

    'Process item here.

    'Update the current item in any bound controls.
    myBindingList.ResetItem(index)
Next
 
Back
Top