web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Hiding Columns in GridView when AutoGenerateColumns is True

Nishant Rana Profile Picture Nishant Rana 11,325 Microsoft Employee

Hi,

If we have set AutoGenerateColumns property of the GridView as true and binding it to a data source, we will not be able to remove columns from it using the Remove or RemoveAt functions of Columns.

myGridView.Columns.Remove or RemoveAt

So to remove the columns we can make use of RowDataBound event.

protected void myGridView _RowDataBound(object sender, GridViewRowEventArgs e){
// hide the second and third column
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Visible=false;

e.Row.Cells[2].Visible=false;}
else
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].Visible = false;

e.Row.Cells[2].Visible = false;}

}
Hope it helps!!


Filed under: ASP.NET Tagged: ASP.NET

This was originally posted here.

Comments

*This post is locked for comments