7

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?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
sa125
  • 28,121
  • 38
  • 111
  • 153

8 Answers8

3

Don't you need to rebind the data grid?

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

?

Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • 1
    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 Oct 13 '09 at 14:32
3

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
  • 12,061
  • 13
  • 49
  • 64
3

this code could be useful:

dataGridView.DataSource = null;
dataGridView.Update();
dataGridView.Refresh();
dataGridView.DataSource = SomeDataSource;

Hope this helps.

Andrew
  • 26,706
  • 9
  • 85
  • 101
2

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 Shah
  • 14,575
  • 16
  • 72
  • 110
  • sorry, not familiar with that (c# newbie). Could you expand a bit? thanks. – sa125 Oct 13 '09 at 14:48
  • 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 Shah Oct 14 '09 at 05:05
2

If I understand you correctly, you want to delete rows selected by a user from your DGV.

  1. Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.

  2. Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)

  3. Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)

I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.

Sam Casil
  • 938
  • 1
  • 11
  • 16
1

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

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
0

Try removing the actual items from your binding source instead.

dell
  • 81
  • 1
  • 1
  • 4
-1

I had same problem and I have find out the root cause. all you have to do initialize you database context object in before reload and see the magic .

Dev 4G
  • 9
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31267372) – MD. RAKIB HASAN Mar 16 '22 at 07:42