0

I am trying to filter data in a DataGridView and I need to hide all rows and columns. I try to do this using looping list LINQ & lambda expressions because if I use a foreach it is slow when I try to filter 3000 rows.

The following code executes without errors and works perfectly in c# but doesn't hide the rows in vb:

c# works perfectly

dataGridView1.Columns.OfType<DataGridViewColumn>().ToList().ForEach(col => col.Visible = false);

vb doesnt works not hide the rows

datagrid.Rows.OfType(Of DataGridViewRow)().ToList().ForEach(Function(obj) obj.Visible = False)

when i change Function by Sub send me error

datagrid.Rows.OfType(Of DataGridViewRow)().ToList().ForEach(Sub(obj) obj.Visible = False)

I'm looking for the equivalent code C# to vb or the action equivalent.

1 Answers1

0

Your first problem is that you're not performing the same action between the c# example and the vb.net examples. Columns can be removed because there is no additional column as there is with rows when Allow User To Add Rows is set to True. This is the row that appears under your data which allows the user to enter information into another row. Using the code you gave using the Sub will work if you set Allow user to add rows to False but you can also just check to ensure that you're not trying to set the EditRow to hidden, which is what is giving the error you talked about but didn't relay to us.

If you need to keep the Allow User To Add Rows set to True, then just use:

datagrid.Rows.OfType(Of DataGridViewRow)().ToList().ForEach(Sub(obj) If Not obj.IsNewRow Then obj.Visible = False)
Charles May
  • 1,725
  • 1
  • 11
  • 18