Gridview Hidden Field,How to get hidden field value in gridview,DataKeys

In this article I will share how to retrieve hidden value from GridView Column.
I had a assignment I need to show only description and not ID.
Following are the steps taken to get it done.

By using Datakeynames property you will get the selected row primary key.

[CODE]

<asp:GridView DataKeyNames=”ID ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” OnRowCommand=”GridView1_RowCommand” OnRowDataBound=”GridView1_RowDataBound” OnRowDeleted=”GridView1_RowDeleted” OnRowDeleting=”GridView1_RowDeleting”>

<Columns>

<asp:TemplateField>
<ItemTemplate>
<asp:Label id=”lblSelect” runat=”server” Text=’<%# Eval(“Description”) %> />
<asp:HiddenField ID=”hdID” runat=”server” Value=’<%# Eval(“ID”) %> />
</ItemTemplate>
</asp:TemplateField>

[CODE BEHIND]

To get ID for selected row.

If you are using RowUpdating, rowdatabound
Dim intId as Integer= GridView1.DataKeys(e.RowIndex).Value

By name :
Dim intId as Integer= GridView1.DataKeys(“ID”).Value

By Index:
Dim intId as Integer= GridView1.DataKeys(0).Value

Please visit my official blog for future article

http://blog.joggee.com