How to get row index by checking a checkbox in a gridview
In this Code i will show how to get row index of the gridview when a check box is clicked.
first,we need a gridview containing checkbox as template column in it.When ever a check box is clicked ,we should get row index for the gridview.
so in design view create a gridview like this
< asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" >
< Columns >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID="chk" runat="server" AutoPostBack="true" Text='< %#Eval("CAT_NAME") % >‘ OnCheckedChanged="Check_Clicked"/ >
< /ItemTemplate >
< /asp:TemplateField >
< /Columns >
< /asp:GridView >
In code behind add this code
protected void Check_Clicked(Object sender, EventArgs e)
{
CheckBox ck1 = (CheckBox) sender;
GridViewRow grow = (GridViewRow)ck1.NamingContainer;
Response.Write("You selected row " + ((CheckBox)grow.FindControl("chk")).Text);
}
In this code we are assigning the naming container for the checkbox ie gridview row to a gridviewrow object .
If u wonder what a naming container is,a naming container is any control that carries the INamingContainer interface.Here the naming container for check box is gridview row.
Excelent!!! ... thank you...
Grettings from Perú