Cynede Cynede - 4 months ago 25
ASP.NET (C#) Question

How to hide columns in an ASP.NET GridView with auto-generated columns?

GridView1.Columns.Count is always zero even SqlDataSource1.DataBind();

But Grid is ok

I can do

for (int i = 0; i < GridView1.HeaderRow.Cells.Count;i++)


I rename request headers here
but

GridView1.Columns[i].Visible = false;


I can't use it because of GridView1.Columns.Count is 0.

So how can I hide them ?

Answer

Try putting the e.Row.Cells[0].Visible = false; inside the RowCreated event of your grid.

protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false; // hides the first column
}

This way it auto-hides the whole column.

You don't have access to the generated columns through grid.Columns[i] in your gridview's DataBound event.

Comments