1

Users noticed that records in the DataGridView started appearing on the screen with some delay.

So I analyzed logs and saw that there is a strong dependency between time that takes it to call DataTable.Rows.Add() and amount of records in data table. If in the beginning it takes less than 0.01 ms, it grows to 1 ms when there about 10000 records in the table and up to 5 ms when 50000. There are two threads: one adds records, the other deletes. DataTable is bounded to DataGridView meaning that it automatically updates gridview every time DataTable rows collection changes.

My understanding that on the amount on records like 100000 in the table there shouldn't be any delays like this. Should I consider implementing VirtualMode for DataGridView? I noticed that VirtualMode property set to true, but CellValueNeeded event not handled.

Code looks like this:

var row = table.NewRow();
//manipulations with row
table.Rows.Add(row);
row.AcceptChanges();

And delete:

var row = table.Rows.Find(id);
if (row != null) {
    row.Delete();
    row.AcceptChanges();
}
Olga
  • 49
  • 5
  • I do not see why you need this line @Olga `row.AcceptChanges();` I have tested your scenario without it and it still adds rows to a datable properly – MethodMan Jan 15 '16 at 17:16
  • The threads are running simultanously? So the grid keeps changing on and on? – etalon11 Jan 15 '16 at 17:23
  • @MethodMan, it was there already. I measured the time for table.Rows.Add(row); it increases, I measured time for row.AcceptChanges(); it doesn't increase, it stays about less that 1 ms. – Olga Jan 18 '16 at 08:22
  • @etalon11, yes, it does. – Olga Jan 18 '16 at 08:25

1 Answers1

0

Yes, you must consider unbinding your DataTable and your DataGridView and using VirtualMode (VirtualMode cannot apply with bound DataSource). You have to bind your DataTable manually in the CellValueNeeded event (Be aware of synchronisation as CellValueNeeded can be handled every time and can lead to index error).

Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • Thanks, @Bioukh! Will try it today. Might take some time as I've never done it before. – Olga Jan 27 '16 at 10:18
  • A link to help : [CellValueNeeded](https://msdn.microsoft.com/fr-fr/library/system.windows.forms.datagridview.cellvalueneeded(v=vs.110).aspx) – Bioukh Jan 27 '16 at 10:30
  • Thanks again, I've implemented VirtualMode, but it doesn't seem help a lot in my situation. The thing is that there are dozens of records being added/deleted to datasource and when this happens, I change RowCount, which causes datagrid add/delete row to datagrid row collection and this operation costs a lot when there are already about 30-40 thousands of records. – Olga Feb 04 '16 at 10:36
  • Now I'm trying to restrict amount of records in grid. Say I only display 1000 records at a time having other records just stay in datatable. In the beginning I still change RowCount until it reaches 1000, when it's 1000 I don't change RowCount and CellValueNeeded doesn't get called, but I want it to be called in order to repaint those 1000 already in the grid (or not all 1000, but only those, which are visible). Summarizing, how do I make grid call CellValueNeeded event without changing RowCount property? – Olga Feb 04 '16 at 10:41
  • I tried RowCount = 0, RowCount = 1000, but it's too slow – Olga Feb 04 '16 at 10:42
  • Did you simply try a simple DataGridView.Refresh() to refresh the cells values displayed ? – Bioukh Feb 04 '16 at 10:47
  • For slow RowCount updates, instead of modifying RowCount, try adding (or removing) only the difference number of rows with the previous refresh (`DataGridView.Rows.Add(54)`) : it may help but I'm not sure. – Bioukh Feb 04 '16 at 10:51
  • The other solution is to init a very big number of rows (more thant you will need) and instead of modifying `RowCount`, just set `Visible = false` for extra rows (add checks in you `CellValueNeeded` handler). – Bioukh Feb 04 '16 at 10:54
  • I tried Refresh, it's still doesn't suffice needs. Now I'm trying to do this every 100 ms if datatable's rowcount didn't change and if did change, change RowCount for Grid, seems to work faster, but can't check on real data if it fast enough yet. – Olga Feb 05 '16 at 12:57