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






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.
By: Sumit Kumar Sharma on October 25, 2007
at 12:40 pm
Thanks for your tip Sumit .
Its also a way to get it done.
I will try and post here.
Keep visiting me.
Rana
By: codeproject on October 25, 2007
at 4:45 pm
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)
.
By: Sharad Kapil Sharma on December 12, 2007
at 4:36 pm
This info was very use full.
thanks for everybody
vijay
By: vijayakumarkiruba on January 9, 2008
at 10:40 am
I got whatever i was searching in simple way!!!
Thanks
By: Sucheta on March 26, 2008
at 6:58 am
Thanks a lot Sumit Kumar, really its a cool ideas.
By: Sumanta P on March 27, 2008
at 9:40 am
thanks.. it worked for me [:)]
By: kamal on April 1, 2008
at 4:28 pm
thank u
thank u
By: Arup on April 20, 2008
at 9:37 pm
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!
By: daniel on June 25, 2008
at 6:11 am
Hi Sharad,
I hope you don’t mind me e-mailing you directly; I found your post on http://codeproject.wordpress.com/2007/10/24/gridview-hidden-fieldhow-to-get-hidden-field-value-in-gridviewdatakeys.
I am trying to use the GridView_RowUpdating command to update data in a row. When I have a BoundField with the ID if my record the update works fine. Howevever, when I set the visibility of the column to FALSE it INSERTS a record rather than updating it. I was wondering if you could take a look at my code below and offer any suggestions?
Here is my code:
Default.aspx
….
Default.aspx.vb
Protected Sub MyGV_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvProjects.RowUpdating
Dim gv As GridView = CType(sender, GridView)
Dim ClientId As String = DirectCast(DirectCast(sender, GridView).Rows(e.RowIndex).FindControl(“ddlClient”), DropDownList).SelectedValue
Dim ResourceId As String = DirectCast(DirectCast(sender, GridView).Rows(e.RowIndex).FindControl(“ddlResource”), DropDownList).SelectedValue
e.NewValues(“ClientId”) = ClientId
e.NewValues(“ResourceId”) = ResourceId
‘ I tried doing this but it didn’t help: e.NewValues(“Id”) = gv.DataKeys(e.RowIndex).Value
Dim i As Integer = 0
While i < gv.Columns.Count
Dim cell As DataControlFieldCell = CType(gv.Rows(e.RowIndex).Cells(i), DataControlFieldCell)
gv.Columns(i).ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, True)
i = i + 1
End While
e.Cancel = False
End Sub
Thanks again for any help you can provide,
Jason
By: Jason on April 7, 2009
at 6:03 pm
Do not forget to add multiple DataKeyNames . In the example above, to retrieve more than one hidden field you should declare them in or using VB, C#, JS …
Cheers,
Cosminator
By: Cosminator on May 15, 2009
at 12:37 pm
hello i am mohammed imran i have to change the gridview column values from arabic to english just by clicking a button canu please solve my problem
By: imran on June 13, 2009
at 12:30 pm
hello i am mohammed imran i have to change the gridview column values from arabic to english just by clicking a button canu please solve my problem
my emailid is:imranwahabit@gmail.com
By: imran on June 13, 2009
at 12:32 pm
%@ Control Language=”C#” AutoEventWireup=”true” CodeFile=”grid.ascx.cs” Inherits=”grid” %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConflictDetection="CompareAllValues"
ConnectionString="”
DeleteCommand=”DELETE FROM [customerfeedback] WHERE [customerid] = @original_customerid AND [customername] = @original_customername AND [CultureID] = @original_CultureID AND [customeraddress] = @original_customeraddress”
InsertCommand=”INSERT INTO [customerfeedback] ([customername], [customerid], [CultureID], [customeraddress]) VALUES (@customername, @customerid, @CultureID, @customeraddress)”
OldValuesParameterFormatString=”original_{0}”
SelectCommand=”SELECT * FROM [customerfeedback]”
UpdateCommand=”UPDATE [customerfeedback] SET [customername] = @customername, [CultureID] = @CultureID, [customeraddress] = @customeraddress WHERE [customerid] = @original_customerid AND [customername] = @original_customername AND [CultureID] = @original_CultureID AND [customeraddress] = @original_customeraddress”>
hello i am mohammed imran i have to change the gridview column values from arabic to english n viseversa just by clicking a button canu please solve my problem,wat alterations i have to do in it
my emailid is:imranwahabit@gmail.com
By: imran on June 13, 2009
at 12:34 pm