Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to hide rows in datagridview winforms

Tags:

c#

winforms

I'm trying to make a window forms application in which I need to hide and show rows of a datagridView quite frequently. Currently and using a loop

for (int i=0;i<grid.Rows.Count;i++){
    grid.Rows[i].Visible = false;
}

But this is making my UI too slow, is there a faster way to do this?? Thanks in advance.

like image 601
Anil Avatar asked Oct 27 '25 10:10

Anil


1 Answers

You can remove all rows after storing them in a list and add them back as needed:

List<DataGridViewRow> rows = null;


// prepare list
rows = new List<DataGridViewRow>();
// copy rows to list
rows.AddRange(dataGridView1.Rows.Cast<DataGridViewRow>());
// remove them all
dataGridView1.Rows.Clear();


// add them back:
dataGridView1.Rows.AddRange(rows.ToArray());
// clean up
rows.Clear();
like image 182
TaW Avatar answered Oct 29 '25 00:10

TaW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!