jbcedge
jbcedge

Reputation: 19515

ASP.NET : Hiding columns in gridview

Is there a way I can control columns from code.

I had a drop drop box with select : Daily and weekend and the gridview column with Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,sunday. If the user selects Daily i want to show columns only from Monday to Friday.

It is possible to control from the code. Oh i am using this griview in my webpage and coding in done using C#.

help!

Upvotes: 6

Views: 11396

Answers (5)

Shakeeb Ahmed
Shakeeb Ahmed

Reputation: 1808

All these code snippet only works when you have AutoGenerateColumns set to false. If you are using AutoGeneratedColumns then you have to loop each row and hide the appropiate cells.

Thank

Upvotes: 3

Merus
Merus

Reputation: 8984

It might be a hassle for you to use the index of the column -- conveniently, the Columns property also accepts the name of the column, which you can set on creation using the Name property of the column. This helps make the code self-documenting.

Upvotes: 0

Matt
Matt

Reputation: 5172

You can programmatically hide or reveal columns by indexing into the Columns collection and setting the Visible property.

For example, to hide the first column in your gridview:

theGridview.Columns[0].Visible = false;

Upvotes: 1

Pavel Chuchuva
Pavel Chuchuva

Reputation: 22475

Use Columns property:

GridView1.Columns[5].Visible = false
GridView1.Columns[6].Visible = false

Upvotes: 8

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

In the Item DataBound event handler sub, for every grid row, check the drop list for "Daily" or "weekend" and then set the visibility of the columns in question to False or true where appropriate.

Upvotes: 1

Related Questions