Tuesday, September 18, 2007

Hidden Columns with values: asp: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='<%# Bind("CompanyCode") %>'
runat="server"></asp:HiddenField></ItemTemplate>
...
// accesing the value property
int tmpID =
Convert.ToInt32(((HiddenField)GridView1.SelectedRow.Cells[3].Controls[1]).Value);


And run it.

1 comment:

Anonymous said...

Thanks a lot. That was very helpful.

Lachezar Bozhkov

Google