Link to home
Start Free TrialLog in
Avatar of digitalZo
digitalZoFlag for India

asked on

GridView specific Column click event

I had posted a question on gridview cell click here: https://www.experts-exchange.com/questions/22880624/GridView-cell-click-event.html

I got my answer and now what I want to know - Is there any way where we can limit the cell click to only ONE particular column - say only the first column. I do not want any click event to happen anywhere on the grid except the first column. Can anyone help?

This is the code [from that link]:


        Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

            Dim gr As GridViewRow

            For Each gr In GridView1.Rows
                Dim tc As TableCell
                For Each tc In gr.Cells

                    tc.Attributes.Add("onclick", "alert('This is ID" & tc.Text & "')")

                Next tc
            Next gr

        End Sub
Avatar of adymono
adymono

try something like

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

            Dim gr As GridViewRow

            For Each gr In GridView1.Rows
              gr.Cells[0].Attributes.Add("onclick", "alert('This is ID" & tc.Text & "')")
           Next gr

        End Sub
Avatar of digitalZo

ASKER

It's throwing an error: Property access must assign to the property or use its value.
ASKER CERTIFIED SOLUTION
Avatar of adymono
adymono

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But 'tc.Text' is not declared because in the original code it is defined as 'Dim tc As TableCell'. How would I get the value of the cell?
Okay thanks I got it. I tried it this way:

For Each gr In GridView1.Rows
                gr.Cells(0).Attributes.Add("onclick", "alert('This is ID" & gr.Cells(0).Text & "')")
Next gr
missed that...

replace tc.Text with
gr.Cells(0).Text

actually...
the tc from your original code is now replaced with
gr.Cells(0)
meaning the first cell in the row
Just another random question: What if I want to limit the click event to selected columns?