• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1222
  • Last Modified:

GridView specific Column click event

I had posted a question on gridview cell click here: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22880624.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
0
digitalZo
Asked:
digitalZo
  • 4
  • 3
1 Solution
 
adymonoCommented:
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
0
 
digitalZoAuthor Commented:
It's throwing an error: Property access must assign to the property or use its value.
0
 
adymonoCommented:
sorry... try this...

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
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
digitalZoAuthor Commented:
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?
0
 
digitalZoAuthor Commented:
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
0
 
adymonoCommented:
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
0
 
digitalZoAuthor Commented:
Just another random question: What if I want to limit the click event to selected columns?
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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