Wednesday, April 3, 2013

hide templatefield column on runtime in gridview

To Hide templatefield column on runtime in gridview:

go to the aspx page of the gridview in the gridview properties search for OnRowCreated event. if not found add that event to the grid.

Example:

<asp:GridView ID="CrowdRatingGrid" runat="server" AutoGenerateColumns="false" OnRowCreated="CrowdRatingGrid_RowCreated" >
...
<asp:TemplateField>
.....
  </asp:TemplateField>
....
   </asp:GridView>

after adding this go to the code behind page and add this method CrowdRatingGrid_RowCreated to handle on row created event.


protected void CrowdRatingGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {...

...

}


Now suppose we have 3 column in our grid and we want to hide the 2nd column then do this:


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

This will hide the 2nd column of the gridview control in asp.net

Protected by Copyscape Duplicate Content Detection Tool

No comments:

Post a Comment