[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 :D
Re: Get data for selected value in gridview
What event are you using to achieve this? Can you post your actual code?
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
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.
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.
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 :p
Thanks all of you guys who try to solve with me.
Thanks :afrog: