views:

1643

answers:

6

Hello,

In my code I need to remove rows from the DataGridView after a recurring interval, and so I call the following function when a timer expires:

private void removeRows(DataGridView dgv) {

    foreach (DataGridViewRow row in dgv.Rows)
    {
        // if some condition holds
        dgv.Remove(row);                
    }
    dgv.Refresh();

}

I know the rows are successfully deleted from the DataGridView, though they still remains in the display for whatever reason. Any tips on what I might be doing wrong?

+3  A: 

Don't you need to rebind the data grid?

dgrv.Datasource = [whatever data source];
dgrv.DataBind();

?

Jack Marchetti
DataGridView is a winforms controls, therefore databind is not needed. If it were ASP .NET, in his example removing rows does not affect the datasource. Therefore doing this will make the gridview show the same data before removing them.
Raúl Roa
+1  A: 

If it's a data-bound grid, you should be working on the binding source itself instead of the grid.

Austin Salonen
A: 

If you have bound your datagrid to an Observable Collection (if not then you should) then you will need to implement INotifyCollectionChanged interface so that listeners are notified of dynamic changes, such as when items get added and removed or the whole list is refreshed.

HTH

Anand
sorry, not familiar with that (c# newbie). Could you expand a bit? thanks.
sa125
Hi, if you edit your post and add some info about how you are populating your datasource, then may be I can help you with some pointers. Cheers.
Anand
+1  A: 

dgv does not contain any Remove method.. anyway are you sure this is not a bag somewhere else in the application? try to reproduce it in a new project.

Itay
The Rows collection has a Remove method. So perhaps it should be:dgv.Rows.Remove(row);But then you're modifying a collection while iterating over it.
Brian Schantz
A: 

Sometimes refreshing the data gridview is not enough and its containing parent should be refreshed too.

Try this:

dgv.Refresh(); // Make sure this comes first
dgv.Parent.Refresh(); // Make sure this comes second

You could also edit your source and attach the new datasource to the control.

Raúl Roa
A: 

Try removing the actual items from your binding source instead.

dell