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

 

 

Advertisements

27 thoughts on “Gridview Hidden Field,How to get hidden field value in gridview,DataKeys

  1. 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.

  2. 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)

    .

  3. 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!

  4. Hi Sharad,

    I hope you don’t mind me e-mailing you directly; I found your post on https://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

  5. 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

  6. 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

  7. 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

  8. %@ 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

  9. can you please explain how to hide a column in c#.net. I’m using a datagrid view to show customer details and want to hide the customer ID field, but want to access the data in that field.

  10. hi every one..i hav a doubt about how to show hidden field in a richtext box in grid view..
    thanks in advance..plz help me out…

  11. Hi!, Please assist. When I use ur code it only pick up the id of the first row only and I need the id for any selected row.

    Please help

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s