How To: Get DataKeys value for multiple DataKeyNames in a GridView for ASP.NET 2.0

Posted: October 1, 2008 in ASP.NET
Tags: , ,

To retrieve the values from a DataKeyNames collection, you can use the following things. You can use the following code to get the DataKeys.

foreach (GridViewRow gvr in gvMyGridView.Rows)
{
string PrimaryKey = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();
}

You can use this code while doing an iteration with “foreach” or for any GridView event like OnRowDataBound.

Here, You can input multiple values for DataKeyNames by separating with comma (‘,’). For example, DataKeyNames="ProductID,ItemID,OrderID"

You can now access each of DataKeys by providing its index like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values[0].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values[1].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values[2].ToString();

You can also use Key Name instead of its index to get the values from DataKeyNames collection like below:

string ProductID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ProductID"].ToString();
string ItemID = gvMyGridView.DataKeys[gvr.RowIndex].Values["ItemID"].ToString();
string OrderID = gvMyGridView.DataKeys[gvr.RowIndex].Values["OrderID"].ToString();

You can also check a similar kind of article here

Happy programing!!

About these ads
Comments
  1. Venkat says:

    Hai
    Nice ,Thank u Very Much for providing service to us

  2. Awais says:

    What a good description? Excellent and Thanks..

  3. Joe says:

    This example helped me a lot. Thanks!

  4. vivek says:

    very good explaination

  5. Sandra R says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  6. manikannan says:

    but its retrieving the last row values only. not the selected row.
    I used one button in gridview row and the row command event, i used ur code.
    It retrieves only the last row values even click the first row.

    Can you help me how to do so….

    • Jeff says:

      Try this:

      int index = grdMyGrid.Rows[e.RowIndex].DataItemIndex;

      GridViewRow gvr = grdImages.Rows[e.RowIndex];

      string SomeValue = grdMyGrid.DataKeys[gvr.RowIndex].Values["ID"].ToString();

  7. keith says:

    this helped..thx in adv

  8. keith says:

    Reply for manikannan question.

    i got your point.Hope this will help on u.

    protected void gvMyGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
    int index = Convert.ToInt32(e.CommandArgument);
    string ProductID = gvMyGridView.DataKeys[index].Values[0].ToString();
    }

  9. Nil says:

    good job!!!!!!!

  10. Ben says:

    Very good post…it help me a lot..

  11. vineeta says:

    thanx a lot this …..code actually works.:)

  12. Mike says:

    Thank you God. You the man. Thanks

  13. amit thakur says:

    Using this code you will be retrevewing selected row value on row command event.
    grid_custom.DataKeys[grid_custom.SelectedIndex].Values["fname"].ToString();

  14. Ling Chang says:

    Thanks so much. This article helps me a lot.

  15. Jeff says:

    It took me a bit of searching to find this – excellent solution and clearly presented. Thanks!

  16. Siva says:

    Thank u very mush. This help’s in right time…….

  17. BC says:

    This was a life saver…wish I had found it days earlier! Thanks!

  18. Luis says:

    Excelent my Friend ¡¡¡…. How Say Scooby Doo ??? … AGUAW ¡¡¡

  19. Denny says:

    Thanks a lot. You are my hero….My issue is resolved….Thanks a lot brother… It was a life saver for me….

  20. Nike says:

    thanx
    MSDN – looser

  21. Jim says:

    simple and Excellent

  22. vishal says:

    thankx very much for this help

  23. vishal says:

    thanks very much for this help

  24. Varun says:

    thank for this piece of code
    very helpful…

  25. Lourenco - Brazil says:

    great!
    amazing… thanks!

  26. jeeva says:

    Hi,

    Thanks for such a wonderful blog..

  27. Espiax says:

    Ud es un maestro… Gracias.

    Saludos

  28. Thanks for providing such a valuable information.

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