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

Posted by Joggee | GridView | Wednesday 24 October 2007 4:38 am

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

 

9 Comments »

  1. Comment by Sumit Kumar Sharma — October 25, 2007 @ 12:40 pm

    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. Comment by codeproject — October 25, 2007 @ 4:45 pm

    Thanks for your tip Sumit .

    Its also a way to get it done.

    I will try and post here.

    Keep visiting me.

    Rana

  3. Comment by Sharad Kapil Sharma — December 12, 2007 @ 4:36 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)

    .

  4. Comment by vijayakumarkiruba — January 9, 2008 @ 10:40 am

    This info was very use full.

    thanks for everybody

    vijay

  5. Comment by Sucheta — March 26, 2008 @ 6:58 am

    I got whatever i was searching in simple way!!!
    Thanks :)

  6. Comment by Sumanta P — March 27, 2008 @ 9:40 am

    Thanks a lot Sumit Kumar, really its a cool ideas.

  7. Comment by kamal — April 1, 2008 @ 4:28 pm

    thanks.. it worked for me [:)]

  8. Comment by Arup — April 20, 2008 @ 9:37 pm

    thank u

    thank u

    :)

  9. Comment by Joggee — June 25, 2008 @ 7:46 am

    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

RSS feed for comments on this post. TrackBack URI

Leave a comment