Learning While Aging

Find the index of the GridView row where embeded CheckBox fired event

Say you have a GridView control with a CheckBox column embedded. When the check box is checked, it fires an event. How can you find out the index of the corresponding row that contains the check box?
Here is the code snippet:

VB.NET code:

Protected Sub chkSelect_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
   Dim cb As CheckBox = CType(sender, CheckBox)
   Dim gr As GridViewRow = CType(cb.NamingContainer, GridViewRow)
   If (gr IsNot Nothing) Then
     Dim i As Integer = gr.RowIndex
   End If
End Sub

C# code:

protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
   CheckBox cb = (CheckBox)sender;
   GridViewRow gr= (GridViewRow)cb.NamingContainer;
   if (gr!= null)
   {
     int i = gr.RowIndex;
   }
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x