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





Hi All,
I have created the Grid view as following and i am able to get the value of that hidden filed. you can also use it.
Its my grid view code.
I have just specified the css class = “invisible” which is as following:
.invisible
{
width:0px;
display:none:
}
Now u can use the row index and the column index for that column and use the value of it.
Thanks for your tip Sumit .
Its also a way to get it done.
I will try and post here.
Keep visiting me.
Rana
Hi,
In this article there are 2 approaches described by rana and Sumit. Thanks for nice codes.
There is an another approach for retrieving hidden cell data from a GridView. I have tested it for a column which is other then a key field and it works fine.
steps…
1. sample.aspx: Consider that you have following field at 4th cell (i.e., index=3) in your gridview, which is not set as invisible at design time..
…
…
…
…
2. sample.aspx.cs:
Add RowDataBound() event for your gridview.
protected void gridview_RowDataBound(object sender, GridViewEventArgs e)
{
…
…
e.Row.Cells[3].Visible = false;
// same for other cells – to set invisible
…
…
}
Note:- This will set the visibility during run time. So no need to use a css file to do the same task.
3. Retrieving data: Just call the data from the cell as follows:
…
…
txtTechPersonName.Text = gridview.Cells[3].Text;
…
…
It works fine for me, hope it will work for you too.
Regards
Sharad Kapil Sharma
(to.kaps@gmail.com)
.
This info was very use full.
thanks for everybody
vijay
I got whatever i was searching in simple way!!!
Thanks
Thanks a lot Sumit Kumar, really its a cool ideas.
thanks.. it worked for me [:)]
thank u
thank u
Danial Asked.”"”"”"
Dim intNewStock As Integer = GridView1.DataKeys(”New_Stock”).Value
Dim intTaked As Integer = GridView1.DataKeys(”Taked”).Value
Dim intReturn As Integer = GridView1.DataKeys(”Return_by_Vendor”).Value
Dim intStockB As Integer = GridView1.DataKeys(”Balance_Stock”).Value
Dim intStockP As Integer = GridView1.DataKeys(”Percent_Stock”).Value
intStockB = (intNewStock – intTaked) + intReturn
intStockP = intStockB / intNewStock * 100
(I have put this code in my update template inside the grid view, i was hoping it would add the fields that i wanted and show on fields i wanted but it didnt happen anyone can help >
Thanks in advance!
Reply::::::::::::::::::::::::
At
Protected Sub gvdVIEW_RowDataBound(
If e.Row().RowIndex -1 Then
Try
Dim lti As Literal
Dim inttotal As Integer = 0
lti = e.Row.FindControl(”total”)
Dim int As Integer = 0
For int = 1 To 5
If e.Row.Cells(int).Text “0″ Then
inttotal = inttotal + e.Row.Cells(int).Text
Else
e.Row.Cells(int).Text = String.Empty
End If
Next
lti.Text = inttotal
Catch ex As Exception
End Try
End If
Please fix loop index as per your coding.
It will work.
Joggee