Results 1 to 6 of 6

Thread: [RESOLVED] Get data for selected value in gridview

  1. #1

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Resolved [RESOLVED] Get data for selected value in gridview

    Hi all,

    I wonder whether I can get the selected data in gridview without using index? Like GridView1.Rows(0).Cells("Name").Text instead of Cells(0).Text bcause who will know which column is at which index right?

    I have total 6 columns in my gridview. (one of them is "Select" column from gridview features and one of them is my DB bind field, "ID" which is in hidden state)

    So, when I try to retrieve the selected row's ID, I got nothing. it returns nothing for this "ID" column. I use .Cells(int index).Text to get the value. (I try index 0 to 6, still no ID value) but when i changed this ID column to visible, i can access it using int index position. strange!!!

    And I try to find whether there is a way to get the value by using column name but i found nothing. (I try to search this forum too, but it reply me a lot of pages and i try to read generally on some pages but still found nothing.)

    So, please anyone guide me or give me some hints or some links for references.

    Thanks. Thanks
    Last edited by scsfdev; Dec 10th, 2008 at 02:35 AM.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

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

    Re: Get data for selected value in gridview

    What event are you using to achieve this? Can you post your actual code?

  3. #3
    Fanatic Member
    Join Date
    Jun 2004
    Location
    All useless places
    Posts
    917

    Re: Get data for selected value in gridview

    I too have tried to get a solution to the index but I doubt I got anything on that (or whether I searched till exhaustion).
    But for what you are trying to achieve I would suggest why not put the ID as the DataKeys property and then you can retrieve the Id value by
    Code:
    For index As Integer = 0 To gvSearchResult.Rows.Count - 1
         Dim currentId As String = gvSearchResult.DataKeys(index).Value
    Next

  4. #4

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Get data for selected value in gridview

    Quote Originally Posted by rjv_rnjn
    I too have tried to get a solution to the index but I doubt I got anything on that (or whether I searched till exhaustion).
    But for what you are trying to achieve I would suggest why not put the ID as the DataKeys property and then you can retrieve the Id value by
    Code:
    For index As Integer = 0 To gvSearchResult.Rows.Count - 1
         Dim currentId As String = gvSearchResult.DataKeys(index).Value
    Next
    Thanks rjv_rnjn, i already got the hidden ID value.
    I use DataKey to get this hidden ID value.
    but now, what I want to get is selected row data.

    I don't want to get it from DB again, it will be a round trip time consuming (I think).
    so, i just simply retrieve the selected data for edit purpose.
    And so, i want to retrieve it by using Column Name instead of Column Index.
    Like GridView1.SelectedRow.Cells("ColumnName").Text
    instead of GridView1.SelectedRow.Cells(ColumnIndex).Text

    thanks.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  5. #5

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Get data for selected value in gridview

    Quote Originally Posted by Besoup
    What event are you using to achieve this? Can you post your actual code?
    Hi Besoup,
    Actually I have a gridview and i use SQLDataSource Control to get the data from SQL server and I got it smooth. And now I want to get the selected row data from this grid. Normally, we will use GridView1.SelectedRow.Cells(columnIndex).Text to get it.
    But now, my point is whether I can get the text value by using ColumnName like GridView1.SelectedRow.Cells("ColumnName").Text???

    I will get the data when the GridView's SelectedIndexChanged event fire in Code Behind (employeeEntry.aspx.vb) file.

    Currently I don't know which code you asked. So, i just explain to u about my current flow.

    Thanks.
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

  6. #6

    Thread Starter
    Addicted Member scsfdev's Avatar
    Join Date
    Feb 2008
    Location
    Singapore
    Posts
    224

    Re: Get data for selected value in gridview

    I somehow manage to solve it.

    I use a way from a guy.
    How i did it is "I give all field names in DataKeyNames value of grid in design view. And then I retrieve this value from CodeBehind by using:
    lblID.Text = gvStaff.SelectedDataKey.Item("id").ToString

    I wish for other people who want to use like me can find this pot

    Thanks all of you guys who try to solve with me.
    Thanks
    I'm using VS 2005 & 2008 & 2010 with SQL Server 2005 Express.

    My hobby beside programming: http://dslrstranger.wordpress.com

Posting Permissions

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



Click Here to Expand Forum to Full Width