Gridview Row Click Retrieve data

I have a Gridview with data. What I want to do is that when I click on a row, it should retrieve the whole row's data and bind it to the textboxes given.

I have tried this code:

 Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load

            Dim gr As GridViewRow
            For Each gr In GridView1.Rows
                Dim tc As TableCell
                For Each tc In gr.Cells
                    tc.Attributes.Add("onclick", String.Format("document.getElementById('Textbox1').value = '{0}'", tc.Text))
                Next tc
            Next gr
        End Sub 'GridView1_Load

. . but it works for only one textbox and one column click. I don't want that. I want that on click of a row, the whole row's data should be binded to the appropriate textboxes and dropdown.

2) Two columns retrieve their data from dropdowns. When I click on the row, the dropdowns should automatically change their selectedvalue as in the grid.

I would appreciate any help on this.

Thanks in advance.
LVL 10
digitalZoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
Try this

 Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load

            Dim gr As GridViewRow
            For Each gr In GridView1.Rows
                Dim tc As TableCell
                For Each tc In gr.Cells
                    tc.Attributes.Add("onclick", "this.value = this.text;")
                Next tc
            Next gr
        End Sub 'GridView1_Load
0
digitalZoAuthor Commented:
Thanks for the quick response but it doesn't work. It doesn't do anything on row click.

I'll have to define the textboxes too somewhere to bind the data to it.
0
Ashish PatelCommented:
you are basically applying the onClick event to TableCell and you are checking on row click? Then how is it going to get fired.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

digitalZoAuthor Commented:
<<you are basically applying the onClick event to TableCell and you are checking on row click? Then how is it going to get fired.>>

Am I? What is the correct solution then?

I want to click on the row and as soon as I click on it, the data of that particular row should get binded to the textboxes or dropdowns.
0
digitalZoAuthor Commented:
<<you are basically applying the onClick event to TableCell and you are checking on row click? Then how is it going to get fired.>>

Sorry. i didn't understand what you meant at first.

i did try clicking on table cell, but it doesn't work either way. i've tried such a code and i dont want it that way. i dont want to click on each table cell for it to bind to the textboxes.

i just want to click on the row anywhere, and the relevant data should bind to the appropriate textboxes or dropdowns.
0
Ashish PatelCommented:
Can you say me what are the names of text boxes in the rows look like?
0
digitalZoAuthor Commented:
I've posted a code in my opening post:

 Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load

            Dim gr As GridViewRow
            For Each gr In GridView1.Rows
                Dim tc As TableCell
                For Each tc In gr.Cells
                    tc.Attributes.Add("onclick", String.Format("document.getElementById('Textbox1').value = '{0}'", tc.Text))
                Next tc
            Next gr
        End Sub 'GridView1_Load

the textbox names could be textbox1, textbox2, textbox3, and so on. The thing is, I want to know how to bind the row's data to the textboxes and dropdowns.
0
digitalZoAuthor Commented:
. . on row click.
0
Ashish PatelCommented:
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
            Dim cnt as Interger = 1
            Dim gr As GridViewRow
            For Each gr In GridView1.Rows
                    gr.Attributes.Add("onclick", "document.getElementById('Textbox" & cnt & "').value = document.getElementById('Textbox" & cnt & "').text;")
                    cnt += 1
            Next gr
        End Sub 'GridView1_Load
0
digitalZoAuthor Commented:
It doesn't work. I click on the row and nothing happens. This is the runtime code generated:

<tr onclick="document.getElementById('Textbox70').value = document.getElementById('Textbox70').text;">
                                          <td style="font-weight:bold;">70</td><td>C</td><td>DC19    </td><td>TV18 HOME SHOPPING NETWORK LTD.                   </td><td>LD0092481</td><td>Pune</td><td>3999</td>
                                    </tr><tr onclick="document.getElementById('Textbox71').value = document.getElementById('Textbox71').text;">
                                          <td style="font-weight:bold;">71</td><td>C</td><td>DC19    </td><td>TV18 HOME SHOPPING NETWORK LTD.                   </td><td>LD0092482</td><td>Nagpur</td><td>3999</td>
                                    </tr><tr onclick="document.getElementById('Textbox72').value = document.getElementById('Textbox72').text;">
                                          <td style="font-weight:bold;">72</td><td>C</td><td>DC19    </td><td>TV18 HOME SHOPPING NETWORK LTD.                   </td><td>LD0092483</td><td>Ludhiana</td><td>3999</td>
                                    </tr><tr onclick="document.getElementById('Textbox73').value = document.getElementById('Textbox73').text;">
                                          <td style="font-weight:bold;">73</td><td>C</td><td>DC19    </td><td>TV18 HOME SHOPPING NETWORK LTD.                   </td><td>LD0092484</td><td>Bhusawal</td><td>3999</td>
                                    </tr><tr onclick="document.getElementById('Textbox74').value = document.getElementById('Textbox74').text;">
                                          <td style="font-weight:bold;">74</td><td>C</td>


I think you misunderstood what I said. I've a set of TextBoxes and Dropdowns. Let's say I've three textboxes - tb1, tb2, tb3.

I've a gridview with data. When I click on a row, the first column data should bind to tb1, the second column data should bind to tb2 and the third column data should bind to tb3. When I click on any other row, the data should replace the textboxes with the clicked row's textboxes.

Hope you understand now.

The code which I posted in my opening post, does this very thing. That is, I click on a row or rather a cell anywhere, and it binds the cell's data to the textbox given.

In this case, it is similar but with the only difference that I want the whole row's data to bind to the appropriate textboxes, and not just one cell [if i click anywhere on the row].
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
digitalZoAuthor Commented:
Sorry a typo:
with the clicked row's data*.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.