Search code examples
asp.neteventsgridviewcheckboxitemtemplate

get gridview column and row from itemtemplate checkbox event


i have gridview with itemTemplate contains a checkbox firing at onCheckChanged. i want to catch the gridview column and row which the gridview is inside.

how can i do it ?

public void PM_chkbx_Chacked_Changed(object sender, EventArgs e)
{
     //What to write here?
}

Solution

  • i have used datakeys in gridview and able to get row and particular column.Here gridview id is grdrequestlist.

    public void PM_chkbx_Chacked_CheckedChanged(object sender, EventArgs e)
    {
    //here you can get row on whcih you checkbox chnged event called
    
    GridViewRow Row = ((GridViewRow)((Control)sender).Parent.Parent);
    string requestid = grdrequestlist.DataKeys[Row.RowIndex].Value.ToString();
    string cellvalue=Row.Cells[1].Text;
    }
    

    This .aspx page design of gridview here you may add your columns in Columns section.

    <asp:GridView ID="grdrequestlist" runat="server" DataKeyNames="RequestId" >
    <Columns>
     <asp:TemplateField HeaderText="Actions" >
                     <ItemTemplate>
     <asp:CheckBox ID="PM_chkbx_Chacked" AutoPostBack="true" runat="server" oncheckedchanged="PM_chkbx_Chacked_CheckedChanged" />
                     </ItemTemplate>
                    </asp:TemplateField>
                        </Columns>
    </asp:GridView>
    

    Hope this helps you.