Hiding Columns in GridView when AutoGenerateColumns is True

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!!


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Hiding Columns in GridView when AutoGenerateColumns is True”

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s