HOWTO: Update a datagrid bound to a collection???

E

Eric J

I need some help around this problem I am having with a datagrid bound to a
collection.

I have a button on my form that adds a record to the collection that is
bound to the datagrid. I want the grid to visibly update as items are
added, so the following code executes each time I press the "add" button:

myDataGrid.DataSource = null;
myCollection.Add(new Foo());
myDataGrid.DataSource = myCollection;

The above code seems like a bit of a hack, but I haven't been able to
discover another way to update the control. In any event, this code works
perfectly until I click in the updated DataGrid and then click on any other
control in the form. When I do that, I am stopped with a messagebox showing
this error:

Specified argument was out of the range of valid values.
Parameter name: Index was out of range. Must be non-negative and
less than the size of the collection. Do you want to correct the
value?

How do I prevent this from happening? I have the feeling it is because I am
not properly updating the datagrid itself. If that is the case, how am I
supposed to redraw the data grid to reflect the change in the data? Calls
to Refresh() and Update() don't seem to work.

Thanks,

-e
 

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