dcsimg
Results 1 to 6 of 6

Thread: Retrieve datakeys value from gridview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    182

    Retrieve datakeys value from gridview

    I have a GridView_RowCommand () that pull the following:

    If e.CommandName = "SelectRetreatant" Then
    Dim index As Integer = Convert.ToInt32(e.CommandArgument)
    Dim row As GridViewRow = GVRetreatant2.Rows(index)
    Dim id As Integer = GVRetreatant2.DataKeys(row.DataItemIndex).Value
    ...

    Because I set my GridView PageSize = 10, it looks like my id value from the above statement fails when I select any records after the first page. What did I do wrong? How can I fix this? Please advise.

    Thank you!

  2. #2
    ASP.NET Moderator mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Retrieve datakeys value from gridview

    Explain fails, what value does it return?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    182

    Re: Retrieve datakeys value from gridview

    For example, if I click on the very first row on Page2, it fails on this line:

    Dim id As Integer = GVRetreatant2.DataKeys(row.DataItemIndex).Value

    where row.DataItemIndex = 10

    with the following error msg: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

    My page size is 10 (index 0-9). According to the above statement, GVRetreatant2.DataKeys(10).value is out of range. I guess somehow it only see my GridView Page size, not the whole GridView Table?

  4. #4
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Retrieve datakeys value from gridview

    try
    GVRetreatant2.DataKeys(row.DataItemIndex)(0).value

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Posts
    182

    Re: Retrieve datakeys value from gridview

    Could you explain why you suggest GVRetreatant2.DataKeys(row.DataItemIndex)(0).value ?

    I tried it, but it doesn't make a difference.

  6. #6
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Retrieve datakeys value from gridview

    Because datakeys is an array the first dimension would be the row of your grid and the second would be the datakey column. You can add multiple datakeys, if you did to access the second one on the list it would be:

    GVRetreatant2.DataKeys(row.DataItemIndex)(1).value

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width