Auto Ad Code

Wednesday, September 28, 2011

Gridview and Hidden Cells (Visible ="false")

This is another frustrating thing developers face while using Gridview instead of the Datagrid. That is when they try to use the value of some hidden cell of Gridview in Code behind they get the empty value. This is obviously very frustrating and have teased me a lot but after Googling and the doing some experiments I got the solution. Whats the solution?
Okay here is the simple solution for this issue
Don't set the Visible property equal to false in design time (HTML code). Instead set visibility to false in RowDataBound event of the Gridview. This will hide the cell but still the value of cell will be available in code behind.
Here is the sample code (let us suppose the Gridview id is gvTest)

VB.Net

Protected Sub gvTest_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTest.RowDataBound
        e.Row.Cells(0).Visible = False
    End Sub


C#
protected void gvRecruiter_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = False
}



This will make Cells[0] to false but still its value will be available in code behind which will not be empty now.

No comments:

Post a Comment