Monday, June 29, 2009

Hidden Columns with values: asp:GridView (access the value of the hidden columns in gridview )

If you set Column Visible property to false, this column won't rendered. But if you want these values available, What will you do?

My trick was, set HeaderText to empty, convert the BoundField in TemplateField, and use a HiddenField control. The effect the column won't be visible. Also you can use the controls array to access to value property.

<columns><asp:boundfield datafield="CompanyCode" headertext="Company" sortexpression="CompanyCode">
...
<columns>
<asp:templatefield><itemtemplate><asp:hiddenfield id="hf1" value="">">
runat="server"></asp:hiddenfield></itemtemplate>
...
// accesing the value property
int tmpID =
Convert.ToInt32(((HiddenField)GridView1.SelectedRow.Cells[3].Controls[1]).Value); </asp:templatefield></columns></asp:boundfield></columns>

No comments: