views:

123

answers:

3

Please Help, I have User Control that contain GridView where i am hiding its Columns on GridView RowCreated event :

    private void gvGrid_RowCreated(object sender, GridViewRowEventArgs e)
    {

                foreach (TableCell objCell in e.Row.Cells)
                {
                   if (objCell is DataControlFieldHeaderCell)
                    {
                            objCell.Visible = false;
                    }
                }

    }

Now every thing was working fine, but On ASPX Page Postback ( containing Griview UserControl), shows all hidden columns, where my PageLoad is as follows

         protected void Page_Load(object sender, EventArgs e)
         {
               if (!IsPostBack)
               {
               ucGridView.PopulatePage();
               }
          }

Note : ASPX Page start working fine if i remove !IsPostBack Check..but i dont want this...Whats the Problem...!

+2  A: 

You need to use the RowDataBound event handler instead of RowCreated.

Wil
A: 

if used in RowDataBound then u cant access griview Column value

salman
A: 

Are you sure you need to make the columns invisible in the code-behind?

If you just need to store field values in the grid but not display them then Microsoft recommends using the DataKeyNames property of the GridView control.

Instead of using code-behind to hide certain columns, you can just remove the bound fields from the GridView and specify them in the DataKeyNames property:

<asp:GridView ID="GridView1" runat="server" 
        DataKeyNames="SalesOrderID,SalesOrderDetailID"
        DataSourceID="LinqDataSource1">

This way the fields will not show to the user but the GridView knows to keep the values around for updating, etc.

YeahStu