[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2915
  • Last Modified:

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.
0
digitalZo
Asked:
digitalZo
  • 7
  • 4
1 Solution
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
digitalZoAuthor Commented:
Sorry a typo:
with the clicked row's data*.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now