Tuesday, July 5, 2011

How to hide a column in Gridview?


If set visible to false in Gridview template, this column will not be rendered into html.  
<asp:BoundField DataField="ID" HeaderStyle-Width="0px" Visible="false" />

 Should add onrowdatabound event handler to do that:
<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow) return;
    e.Row.Cells[1].Visible = false;
}

No comments:

Post a Comment