How to hide Gridview column programmatically?
Posted by Ramani Sandeep 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;
}
}
Hope this will help you !!!
Happy Programming
This entry was posted on April 7, 2009 at 3:15 pm and is filed under ASP.NET, C# 2.0. Tagged: Gridview - Hide Column, Hide Column of Gridview, hide gridview column in asp.net, 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! ^_^
Karthick said
Its absolutely working
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..
Gridview – insert, update, delete « Web Developer Friends said
[...] How to hide Gridview column programmatically? [...]
Sunit said
Great post. Thanks alot
Terry said
How do you pick a certain column by name to hide, for example, I have the columns Make Model and Serial Number, how do I had the Make column?
Terry said
How do you pick a certain column by name to hide, for example, I have the columns Make Model and Serial Number, how do I hide the Make column?
Ramani Sandeep said
Hi Terry,
Access the gridview column by its index no like
if Make Model is first place than
access it with [0] , if at second place than [1] like wise you can access it.
Hope this help
Terry said
Is there a way to go through the grid view and get the indexes based on a name? I am adding columns based on the user selecting items in a list view. As they select the items, the columns are added to the grid view. If they select them again i would like to have the columns disappear, insted it adds them twice. If I could look though all the columns in the grid view and get the indexes then I would be able remove the colunn instead of adding it twice.
Tarun said
4 me following code works to hide the coloumn
Grd_show.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in Grd_show.Rows)
{
gvr.Cells[0].Visible = false;
}
as i m generating the grid at run time so the following code does not work i do not know y so?
GridView1.DataBind();
02 if (GridView1.Columns.Count > 0)
03 GridView1.Columns[0].Visible = false;
sunil said
Hi Sandeep,
I’ve problem in hiding a row of girdview which is inside the datalist.suppose if the gridview row contains no data then it should be visible false.can u please help me out of this
Ramani Sandeep said
I am not getting meaning of this “gridview row contains no data”. why such row exist in selection of the table & can’t you supress it from the DB selection so that no other processing required…
Hope this help!!!
la jiao shou shen said
Great post. Thanks alot
Osman said
Thank you
Sourabh S. Malani said
Hello Friend,
For hide column in gridview
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Index of column to be hide
e.Row.Cells[0].Visible = false;
}
if(e.Row.RowType == DataControlRowType.Header)
{
// Index of column to be hide
e.Row.Cells[0].Visible = false;
}
}
I hope this will help you.
Thanks & Regards,
Sourabh S. Malani
Fx15 said
thanks reply
Shreya said
Thanks a lottt………….!!!!!
It worked….!!!
Minh Nhat Hoang said
Thanks so much, now I can complete my Assignment
kalai said
I use this code successfully hide column but my page index also hided… So there may be a issue in this code…
dinesh said
if i click once a radiobutton gridview want to visible on clicking another radiobutton first grid view1 gets hide and grid view2 wants to get visible please send me source code for this
John giblin said
anyway of using a name to get the column?
Ramani Sandeep said
not getting what you are asking?
Darshan Hegde said
Thanks a lot.Matter of fact it makes sense to keep checking out with cells at autogeneratecolumns true in case of data binding!
Tiago Bento said
Thank you very much! I´ve been trying to fix an error for a total of 3 Hours and you saved me with a couple of lines. Again, thank you!
bonrc said
for (int i = 0; i < GridView2.Columns.Count; i++)
{
if (GridView2.Columns[i].HeaderText == "Total Hours")
{
GridView2.Columns[i].Visible = false;
}
}
SIVANEE said
I want to hide only the third column of the grid view
Ruby said
Great ! this really helped me !!! Thanks
sajid said
Excellent !
Wilmer Jesús González Pacheco said
Thank you very much!
Shekar said
Thank You… I was looking exactly for this…
Abdul Basit said
Thanks a lot…..
suryanaryana said
thank u man
Serkan said
Serkan…
[...]How to hide Gridview column programmatically? « Ramani Sandeep's Blog[...]…
Devloper's Solution said
thanks buddy it’s really good.
Pravin Varade said
How to hide column in nested gridview asp.net?
My second gridview is in itemtemplate of first gridview.
Ramani Sandeep said
Hi Pravin, First of try to find the Gridview Contron which is in ItemTemplate. Use the following type of code :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals(“Update”))
{
int index = int.Parse(e.CommandArgument.ToString());
GridViewRow row = GridView1.Rows[index];
GridView gvState = (GridView)row.FindControl(“GridViewID”);
//Use the gvState Gridview and hide the columns you want to hide using above code psoted for simple grid.
}
}
Hope this will help !!!
msbuzzz said
Great Stuff!!
Pravin Varade said
Thanks Boss!!!… Great… This Solution solves my problem.
Ramani Sandeep said
Anytime pravin… I am glad to know this…
NARAYAN said
Cooooooooooooollllllllll
NARAYAN said
Simple and great. Thanks
Arthur Savage said
function hideCol(columns){
//col_num = 0 //document.getElementById(“column_numbder”).value;
rows = document.getElementById(“GridView1″).rows;
for(i=0; i<rows.length; i++)
{ rows[i].cells[columns].style.display="none"; }
return true;
}
//
function showCol(columns){
//col_num = 0 //document.getElementById("column_numbder").value;
rows = document.getElementById("GridView1").rows;
for(i=0; i<rows.length; i++)
{ rows[i].cells[columns].style.display=""; }
return true;
}
krishna said
when i scroll my scrollbar Div half way down , then trigger a reload , i want it tries to find the last scroll position. instead of go back to the top
Rutika Patel said
hello sandeep
i want to call java script function from code be-hide of asp.net
Page.ClientScript.RegisterStartupScript(this.GetType(), “test”, string.Format(“javascript:SetCookies()”));
i can wright as under given
but its not working… or not call this function please tell me what is problem
Lizzette Schaer said
I believe other website proprietors should take this site as an example , very clean and good user friendly design and style .
Laura said
I know this post is very old, but you have no idea how this helped me! thank you so much!
linkonbaruaiiuc said
Many many many tx for your post
Pravin Varade said
Rdlc Report not display properly in Google Chrome but work fine in IE. How to set width in columns of table in RDLC Report which display properly in all browser?