3

I have a DataGridView control on my windows application form. This DataGridView is populated on the basis of a plain text file (specified by user at run time). Hence the number of columns and rows are calculated dynamically. Now the every thing works fine as expected the only issue is that my DataGridView took a lot of time to load data, is there any way to optimize the performance of DataGridView?

Hint: Normally the datagridview contians 1024 columns and almos 100 rows.

Following is the code for populating my DataGridView

dataGridView1.ColumnCount = nColumnCount;

for (int i = 0; i < CurrPageLines.Length; i++)
{
    string sCurrLinecontents = CurrPageLines[i];
    int n = dataGridView1.Rows.Add();
    for (int j = 0; j < /*nColumnCount*/sCurrLinecontents.Length; j++)
    {
        dataGridView1.Rows[n].Cells[j].Value = sCurrLinecontents[j];
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jame
  • 21,150
  • 37
  • 80
  • 107
  • 1
    Are you sure you need 1024 columns? What user is going to read all that? – Emond Apr 14 '11 at 06:07
  • 1
    I think, you should divide this grid to a lot of grid, nobody read 1024 columns.. – Soner Gönül Apr 14 '11 at 06:09
  • Load enough to make the grid look like data is loaded. Continue loading in the background on another tread. Then load the other data when the user looks for it. – Jon Abaca Apr 14 '11 at 06:16
  • @Jon your suggestion is good when there is lot of rows not significant when there a lots of columns, coz one row for all columns – V4Vendetta Apr 14 '11 at 06:33
  • @Erno @Soner Gönül there is only single character in each cell and there are both horizontal and vertical scroll bars aswell – Jame Apr 14 '11 at 06:53
  • Even a single character (assume 8 pixels for a character) 1024 * 8 + 1024 * margins and vertical lines = way too much to scroll. Feels like Excel... – Emond Apr 14 '11 at 06:59
  • Make sure you add `dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;`. You can always reenable it afterwards. ` – Dan W Sep 10 '12 at 03:18

1 Answers1

6

There is an article on MSDN on how use the virtual mode of the datagridview. It even comes with a nice walkthrough example. Although it seems to be more targeted towards lots of rows instead of lots of columns it might still be useful.

Update: If you are just experiencing delays when loading data you might be able to improve things by creating a DataTable or a BindingList from your textfile and then bind that to the view.

ChrisWue
  • 18,612
  • 4
  • 58
  • 83