Search code examples
c#asp.netcheckboxtelerik-grid

get the value of each check marked check box inside rad grid in code behind


In my radgrid I am using check box to select the required values from the multiple options. In my code behind, I want to get the value of every checked rows. But my loop is not working.

Also I am not sure whether I am using the correct approach for the checkbox inside rad grid. Please suggest.

<telerik:RadGrid ID="rg_get_profession" runat="server"
PageSize="100"
AllowSorting="true"
Width="100%"
AllowPaging="True"
ShowGroupPanel="false"
AutoGenerateColumns="false"
GridLines="Both"
HeaderStyle-HorizontalAlign="Left" >
<MasterTableView AutoGenerateColumns="false" AllowFilteringByColumn="false" ShowFooter="false">
    <Columns>
        <telerik:GridTemplateColumn UniqueName="TempCol1" HeaderText="Select">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBox ID="CheckBox2" runat="server" />
            </EditItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn DataField="p_id" Display="false" Visible="true" AutoPostBackOnFilter="false" CurrentFilterFunction="Contains" />
        <telerik:GridBoundColumn DataField="p_name" HeaderText="Name" AutoPostBackOnFilter="false" CurrentFilterFunction="Contains" />     
    </Columns>
</MasterTableView>
</telerik:RadGrid>

What I want to do is to insert the "p_id" for each checked rows in the database. But my loop is not working. Please suggest how can I achieve this.

code behind >>

foreach (GridDataItem item in rg_get_profession.Items)
{
    CheckBox chkbox = item.FindControl("TempCol1") as CheckBox;
    if(chkbox != null && chkbox.Checked)
    {
        string id = item.Cells[1].Text;
    }
}

While debugging the code, the execution does not go inside the if block though I checked multiple items in the rad grid check box. I tried doing ::

foreach (GridDataItem item in rg_get_profession.MasterTableView.Items)
    {
        CheckBox chkbox = item.FindControl("CheckBox1") as CheckBox;
        if (chkbox != null && chkbox.Checked)
        {           
            string id_ = item.Cells[1].Text;            
        }
    }

Now when I put check mark in the check boxes, my execution goes inside the loop if (chkbox != null && chkbox.Checked) but I can not access the value of p_id. string id_ = item.Cells[1].Text; is not giving the value of p_id.

How can I access the value of p_id which are check marked? Please suggest.


Solution

  • I changed the item.Cells[1].Text; to item["p_id"].Text;. Code snippet to get the value of the datafield ::

    foreach (GridDataItem item in rg_get_profession.MasterTableView.Items)
        {
            CheckBox chkbox = item.FindControl("CheckBox1") as CheckBox;
            if (chkbox != null && chkbox.Checked)
            {           
                string id_ = item["p_id"].Text;            
            }
        }
    

    I can get the value of the p_id which are checked mark.