How to hide Gridview column programmatically?
Posted by ramanisandeep on April 7, 2009
Question : How to hide Gridview column programmatically?
Solution :
The Columns collection only stores the explicitly declared columns, so if you’re using autogenerated columns, the count will be zero.
If you’re using autogenerated column, after databind you could loop through the rows collection and make the appropriate cells invisible, like:
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
GridView1.Columns[0].Visible = false;
else
{
GridView1.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[0].Visible = false;
}
}
Possibly related posts: (automatically generated)
This entry was posted on April 7, 2009 at 3:15 pm and is filed under ASP.NET 2.0, C# 2.0. Tagged: Gridview - Hide Column, Hide Column of Gridview, How to hide Gridview column programmatically?. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
kaka said
Thanks for sharing this.
I’ve been looking how to hide the column… and it works! ^_^
Greg said
Thanks a lot!!!!
Great explanation!
Works like a charm!
Shivraj said
coollllllllllllllllllllllllll
Solution
Greattttttttttt Job ramanisandeep
Ajit said
Hi,
How to hide a column ( Primary key) in Data Grid View if they are later used for editing purpose on click of context menu.
Michael said
Hi
I’m filling up gridview with the data and then hide some columns.
For one column I change header by the value from one cell from the same row. Everything goes fine but when I’m hiding a column where I just taken a text for header I’m losing a header text as well.
I very appreciate your help.
ramanisandeep said
if u want to retain header text then save it in hidden field.
if you save it in header & when we hide header we make it visible false..it means it does get rendered so you will not have it value.. so try hidden field..it will work..