1
votes

I'm trying to update data I have in a datagridview (based on checks set by the user), without losing the selection (on which a calculation is made). This works for me, but is a lot slower than clearing the complete dgv, making a new datatable and binding it.

This is what I tried :

  • when the datagridview need to be completely updated (different amount of rows), I clear the datagridview, make a new datatable and bind it. This works nice and fast, but the selection is lost. It's no problem the selection is lost, because the data is completely new.

  • sometimes I only need to update the values in the datagridview (and I don't want to lose the selection). I tried to either update the values in the bound datatable (works) or the values in the datagridview directly (this also works). However, both are a lot slower than just making a whole new datagridview using the first method :-(

  • when updating the datagridview, I stop drawing the panel on which it is placed

  • all events that the datagridview fires are suspended
  • autoresize etc is off

Can anyone explain why updating the cellvalues is so much slower than redoing the whole thing?

Is there another way to do it without losing the selection? I could remember the selection and set it again, but then I lose the way/order cells and columns where selected.

1

1 Answers

0
votes

I finally found where the big delay occured. I was completely focused on the DataGridView, but it turned out the delay happend in the DataTable that was bound to it.

In order to do it fast :

  • first set each row of the DataTable to BeginEdit, so changes are not acted upon. After updating the DataGridView, do not make your DataTable accept the changes, it will make you lose the selection in the DataGridView.

[code]

for (int Row = 0; Row < MyDataTable.Rows.Count; Row++) MyDataTable.Rows[Row].BeginEdit();

// do your changes in the DataGridView (not the DataTable) here
UpdateDataGridView();

// never accept the changes in the DataTable, it will lose the selection in the DataGridView. Problem is that the DataTable is never updated, but this is not a problem in my case.
//MyDataTable.AcceptChanges();

[/code]