3

I don't want to loop through all the columns and set each column's Visible to false. I wonder if there is a quick way to do so.

Thank you!

King King
  • 61,710
  • 16
  • 105
  • 130
  • 1
    I'm not aware of any solution beside making the whole grid invisible. But a `grid.Columns.ForEach(g => g.visible = false)` would probably be the shortest way to write the loop. – Chief Wiggum May 21 '13 at 03:21
  • @JamesBlond I don't intend to hide all the grid, I just want to hide all its columns quickly then I just set a few columns of it visible, like myGrid.Columns["ID"].Visible = myGrid.Columns["Name"].Visible = true. It's much cleaner. Thank you for your short way though it's still some kind of loop. – King King May 21 '13 at 03:33
  • Yes, it's still a loop, but a short one liner... – Chief Wiggum May 21 '13 at 03:38
  • @KingKing well if you can't turn off the datasource any solution regarding this would involve a loop since you are targeting the visible property of individual columns in the collection for which you would need to visit them atleast once – V4Vendetta May 21 '13 at 03:56

5 Answers5

6

Also you can use LINQ as below:

dataGridView1.Columns.OfType<DataGridViewColumn>().ToList().ForEach(col => col.Visible = false);
Ali
  • 3,373
  • 5
  • 42
  • 54
2
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

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

set data source to null, when you want to show it again, you can set the data source back.

Or you can set Gridview visible false or gridview containing control to visible false.

Damith
  • 62,401
  • 13
  • 102
  • 153
0

Have two grids the same exact size and location.

if(conditionMet)
{
   grid1.visible = false;
   grid2.visible = true;
}
0

Old Question thought it might be helpful for someone !! This might be an easy option..

foreach (DataGridViewColumn col in myDgv.Columns)
{
    col.Visible = false;
}

as well as you can iterate through rows..

foreach (DataGridViewRow row in myDgv.Rows)
{
    // your code
}
shakee93
  • 4,976
  • 2
  • 28
  • 32