Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - how do I refresh DataGridView after removing rows

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?

like image 365
sa125 Avatar asked Oct 13 '09 14:10

sa125


2 Answers

Don't you need to rebind the data grid?

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

?

like image 198
Jack Marchetti Avatar answered Sep 21 '22 17:09

Jack Marchetti


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.

like image 41
Raúl Roa Avatar answered Sep 19 '22 17:09

Raúl Roa