0

I'm loading rows from a file into a DataGridView. The file has 25000 rows.

The loading takes enough time that I added a progress bar to the GUI (updating every 128 rows).

Is there a method or process to reserve rows before I start using the Add method?

I'd like to speed up the adding of data rows.

Environment:
C# -- Visual Studio 2017
Windows 10

P.S. All my searches are about adding a single row to the DataGridView. I didn't find anything about pre-allocating or reserving space.

For those needing a sample code

String[] items = file.Split(',');
List<string> list = new List<string>();
DataGridView data_table;
foreach (String item in items)
    list.Add(item.Trim());
for (int i = 0; i < list.count; ++i)
{
    string time_interval_as_string = list[i];
    uint   time_interval = 0;
    uint.TryParse(time_interval_as_string, out time_interval);
    string energy_value_as_string = list[++i];  
    double energy_value = 0.0;
    double.TryParse(energy_value_as_string, out energy_value);  

    dataTable.Rows.Add(time_interval, energy_value);

}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • There's no such thing as `DataViewGrid` in WinForms or WPF - don't you mean `DataGridView` instead? – Dai Jul 11 '22 at 16:47
  • Use AddRange instead. Add is repainting the control after each item making it very slow. – jdweng Jul 11 '22 at 16:48
  • @jdweng `DataGridView` doesn't repaint on every `Add`: it only repaints rows visible in the control's viewport _and_ only in response to a `WM_PAINT` message after invalidation (which never happens inside user-code unless they call `DoEvents`), so calling `Add` twice will only invalidate the control _once_ and then repaint _once_. But I do agree that `DataGridView` does have poor performance by default though. – Dai Jul 11 '22 at 16:49
  • 2
    Please show [how](https://stackoverflow.com/q/10063770/1997232) you add rows and provide more background. Are you trying to implement [virtual mode](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/implementing-virtual-mode-wf-datagridview-control)? – Sinatr Jul 11 '22 at 16:50
  • 1
    Populate a DataTable or some other list and assign that to the DataSource property of the grid. – John Jul 11 '22 at 16:52
  • @Dai : Are you sure???As you add rows the data is going to scroll and rows in view are going to change. When do the rows get validated? – jdweng Jul 11 '22 at 17:00
  • IMHO, the repainting or refreshing is not the delay factor. The table is not refreshed until after all items are loaded into the table. – Thomas Matthews Jul 11 '22 at 17:05

0 Answers0