2

I've made a DataGradView in C# which allows me to collapse and expand rows. Looks like this:

enter image description here

At startup i want to collapse all customers with multiple entrys. Like the one shown in picture. Works well if the total number of customers is < 10.000 If total number of customer is > 10.000 it takes a few minutes to go.

Code to collapse all customers:

   int number = 0;
        int oldnumber = 0;
        int parent = 0;

        foreach (CustomDataGridViewRow row in dataGridView1.Rows)
        {
            try
            {
                number = int.Parse(row.Cells[1].Value.ToString());

                if (number != oldnumber && int.Parse(dataGridView1.Rows[row.Index + 1].Cells[1].Value.ToString()) == number)
                {
                    row.Is_expander = true;
                    parent = row.Index;

                }
                if (number == oldnumber)
                {
                    row.hide(parent);
                }
                oldnumber = number;
            }
            catch { }
        }

foreach row which has to collapse i call row.hide(parent), which set the visibility to false and saves the parent index.

foreach parent row an icon will be drawn

Can you guess why it takes so long to hide all these rows? Maybe after each hidden row screen draws again? Maybe i am doing unnecessary or heavy-performance stuff in loop? I have no idea

5 Answers 5

5

Hiding and showing rows on a datagridview is very slow for a reason only microsoft knows, I guess. However, copying the rows to an array, manipulating them in the array and copying them back to the grid is very fast. Here is some code:

DataGridViewRow[] theRows = new DataGridViewRow[Adgv.Rows.Count];
Adgv.Rows.CopyTo(theRows, 0);
Adgv.Rows.Clear();
for (int loop = 0; loop < theRows.Length; loop++) theRows[loop].Visible = false;
Adgv.Rows.AddRange(theRows);

This gets done in less than a second for 35000 rows. In my code I usually have an expression that defines whether the row is to be hidden or shown and it is with the expression that it gets executed in less than a second. If I attempt to do that operation without copying it takes a few minutes. Go figure!

2
  • 1
    Of all that I have tried, this one worked for me (non-data tabled DatagridView) Thank you.
    – MwBakker
    Commented Nov 28, 2017 at 11:07
  • This is the only thing that worked for me. Note that this will reset the selection however, so if you need to retain it you must store and restore that manually.
    – Vapid
    Commented Nov 16, 2021 at 7:25
3

You can use VirtualMode with DataGridView in order to very efficiently update the grid. See this article: http://msdn.microsoft.com/en-us/library/ms171622.aspx

If you are rendering > 10,000 virtual-mode is a must!

3
  • 2
    Just setting virtualmode = true did not shows any result. btw: it also seems that it takes exponentially time to hide all rows. 5000 rows in 2 seconds and 20.000 rows in 2 minutes Commented Feb 13, 2012 at 12:46
  • 1
    Read the referenced article, virtual mode is more than just a boolean property. You have to handle the various events that are fired to populate the visible window of cells. I have created a grouped grid that can render 100,000 + rows using this approach.
    – ColinE
    Commented Feb 13, 2012 at 13:16
  • which events exactly are fired when hiding a row? Do you hiding or removing rows? Ive found out that removing and adding is more efficent then hiding Commented Feb 14, 2012 at 13:36
1

Is that necessary to select all 10000 rows from the start. It is possible to display data in different pages starting from 1-9,10-19,20-19 etc. It will boost performance and also provide system of paging. Why can't you try to modify the query ?

2
  • It is necessary to select all 10000 rows from the start Commented Feb 14, 2012 at 14:35
  • 1
    then its better to do calculations inside sp/sql and returns the dataset and assign it to gridviews datasource
    – kbvishnu
    Commented Feb 14, 2012 at 17:30
1

If the autosize is set, there is a problem with the Grid. On every change of the visible - attribute, the complete grid is checked for the perfekt size.

Try out these:

Grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;

foreach (DataGridViewRow Row in Grid.Rows) 
{
   if (......) 
   {
     Row.Visible = false;
   }
}
Grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
1
  • You have to do this for each column if it should work Commented Sep 8, 2021 at 7:38
1

In my case dataGridView was very slow when changing the dataGrid1.Rows[z].Visible property. The solution was to turn the AutoSize off temporarily:

for (int z = 0; z < dataGridView1.Columns.Count; z++)
{ // Disabled AutoSize Mode for all columns
  dataGridView1.Columns[iRow].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
}

for (int iRow = 0; iRow <= dataGridView1.RowCount; iRow++)
{ // Filtering dataGridView1
  bool bVisibleCondition = ...
  dataGridView1.Rows[iRealRow].Visible = bVisibleCondition;
}

for (int z = 0; z < dataGridView1.Columns.Count; z++)
{ // Enable AutoSize Mode for all columns
  dataGridView1.Columns[z].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.