Donovan Keating
Donovan Keating

Reputation: 1885

C# DataGridView - Choosing which columns to display

I have a DataGridView where its data is retrieved from a MySQL database. How do I only show columns to display? One way is to use the Visible property of the Columns object and set dgv.Columns["col"].Visible = false;

But I want to do it the other way around. I want to choose which columns to display, not choose which columns to hide. Something like this: dgv.Columns["col"].Display= true;

Upvotes: 0

Views: 1289

Answers (2)

dreojs16
dreojs16

Reputation: 117

how about first setting all columns to invisible and then setting the ones you want to visible?

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

     dgv.Columns["this_one_i_want_to_see"].Visible = true;
     dgv.Columns["this_one_i_want_to_see_too"].Visible = true;

Upvotes: 0

Jesse Johnson
Jesse Johnson

Reputation: 1718

Try setting the AutoGenerateColumns property on the DataGridView to false. This will require you to specifically set myColumn.Visible = true for columns you would like to display.

Upvotes: 1

Related Questions