King King
King King

Reputation: 63317

Is there a quick way to set all columns of DataGridView invisible?

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!

Upvotes: 3

Views: 8899

Answers (5)

Ali
Ali

Reputation: 3469

Also you can use LINQ as below:

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

Upvotes: 6

shakee93
shakee93

Reputation: 5386

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
}

Upvotes: 0

Sam Samangi
Sam Samangi

Reputation: 21

dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;

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

Upvotes: 2

Matt McHone
Matt McHone

Reputation: 11

Have two grids the same exact size and location.

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

Upvotes: 0

Damith
Damith

Reputation: 63065

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.

Upvotes: 0

Related Questions