If Gridview has any BoundField column for which visible property is set to "false" then that columns won't be rendered at runtime and you can not get value of hidden column.
if you try to get value of any cell of hidden column by using GV.Rows[i].Cell[0].Text then it returns nothing.
But there are Four ways to get value of hidden column -
I am taking an example of gridview that display addresses and has first column for "AddressID" that is hidden column and I am defining different ways to get value of "AddressID".
1. First Way -
(a). In Gridview set DataKeyNames property to hidden column id.
Example:
In following case set DataKeyNams to "AddressID" and set visible false to first column that displays "AddressID".
<asp:GridView ID="GVAddress" runat="server" AutoGenerateColumns="False" DataKeyNames="AddressID" >
<Columns>
<asp:BoundField DataField="AddressID" HeaderText="AddressID" Visible = "false" />
<asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />
<asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />
</Columns>
</asp:GridView>
(b). Value of hidden Column i.e. AddressID can be get by using DayaKeys property of Gridview.
GVAddress.DataKeys[0].Value.ToString();
2. Second Way -
(a). Create TemplateField column for hidden column in GridView,Add Label inside <ItemTemplate> And set visible to false for <asp:TemplateField>. So that TemplateField column won't be displayed at runtime.
<asp:GridView ID=" GVAddress" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField Visible=false>
<ItemTemplate>
<asp:Label id="lblAddressId" runat ="server" text='<%# Eval("AddressID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />
<asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />
</Columns>
</asp:GridView>
(b). Use FindControl method of Row property of GridView to find Label control defined inside TemplateField column and assign it to label object and get value of label by using text property.
Label lblAddressId = (Label) GVAddress.Rows[index].FindControl("lblAddressId");
if (lblAddressId != null)
{
string strAddressId = lblAddressId.Text;
}
3. Third Way -
(a). Create a stylesheet for hiding grid column.
<style type="text/css">
.hideGridColumn
{
display:none;
}
</style>
(b). Set HeaderStyle-CssClass and ItemStyle-CssClass property of column to hideGridColumn, that is defined in css, to hide column "AddressID" column.
<asp:GridView ID="GVAddress" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="AddressID" HeaderText="AddressID" HeaderStyle-CssClass=hideGridColumn ItemStyle-CssClass = hideGridColumn />
<asp:BoundField DataField="AddressLine1" HeaderText="AddressLine1" SortExpression="AddressLine1" />
<asp:BoundField DataField="AddressLine2" HeaderText="AddressLine2" SortExpression="AddressLine2" />
</Columns>
</asp:GridView>
(c). Get value of hidden column by using Cell property.
GVAddress.Rows[0].Cell[0].Text
4. Fourth Way -
Set visible to false of coulmn that is reuired to hide after bininging datasource to Gridview in Codebehind. In that case value of hidden column will be available by using GVAddress.Rows[0].Cell[0].Text.
GVAddress.DataSource = dtAddress;
GVAddress.DataBind();
GVAddress.Columns[0].Visible = false;