Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

ASP.NET GridView

When you bind a GridView to an ObjectSource in ASP.NET, sometimes, you don't want to display all of the columns returned from the ObjectSource DataTable, but if you use a GridView and let it autmatically generate the columns, it becomes not a striaght forward deal to hide the individual selected columns. Here is a way to do that:

Simply catch the RowDataBound event on the GridView and if you know the index of the column to hide, just use it as follows:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.Cells.Count >= 1)
        e.Row.Cells[1].Visible = false;
}

Assuming you want to hide the second column in your automatically generated GridView.

kick it on DotNetKicks.com

Feedback

Posted on 6/26/2012 7:24:50 AM

just write
e.Row.Cells[0].Visible = false;
in RowDataBound event
It will work

Posted on 7/9/2010 10:41:17 AM

Hello Friends,

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

Posted on 9/11/2008 3:00:47 AM

This will hide the row only but what about Header??

Posted on 6/24/2008 10:39:27 AM

This is only hiding the datarow cells and not the header row?

Posted on 12/27/2007 12:05:34 AM

Just set AutoGenerateColumns to false (property on the gridview) , then bind whatever you need like : asp:BoundField DataField="...

Posted on 12/26/2007 11:20:59 PM

Sid: that would require me to add a bound field column in advance to my DataGrid and bind it to a corresponding DataField, however, my GridView is populated automatically with columns.

Posted on 12/26/2007 3:54:57 PM

Why not just set the Visible property of the fields in the ASPX page?

<asp:BoundField Visible = "false" />

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author