How to hide a column (GridView) but still access its value?
<head runat="server">
<title>Accessing GridView Hidden Column value </title>
<style type="text/css">
.hiddencol
{
display: none;
}
</style>
<asp:BoundField HeaderText="Email ID" DataField="EmailId" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" >
</asp:BoundField>
ArrayList EmailList = new ArrayList();
foreach (GridViewRow itemrow in gvEmployeeDetails.Rows)
{
EmailList.Add(itemrow.Cells[YourIndex].Text);
}
Define a style in css:
.hiddencol { display: none; }
Then add the ItemStyle-CssClass="hiddencol"
and the HeaderStyle-CssClass="hiddencol"
attribute to the grid field:
<asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" ClientIDMode="Static" />
If I am not mistaken, GridView
does not hold the values of BoundColumns
that have the attribute visible="false"
. Two things you may do here, one (as explained in the answer from V4Vendetta) to use Datakeys
. Or you can change your BoundColumn
to a TemplateField
. And in the ItemTemplate
, add a control like Label
, make its visibility false and give your value to that Label
.