Search code examples
c#asp.netteleriktelerik-gridradgrid

Use DataKeyNames and CheckBox in GridTemplateColumn to see what rows have been selected?


We have a webform with a radgrid and a button. The radgrid has two columns and a checkbox column.

On Page_load, the data will be displayed and the user will be able to select (via the checkbox) which rows will go through with the update of the EmpId; if the row is selected, NewEmpId will replace OldEmpId. The Update button is clicked and the changes are made.

The problem I'm having is that I prefer to use the datakeynames to retrieve the data from each row. But I'm having trouble implementing it without using GridCheckBoxColumn, which I don't want to use because the radgrid needs to be in Edit mode for the checkbox to be selected, and that brings other issues.

<telerik:RadGrid ID="RadGridEmp" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" PageSize="22">
    <ClientSettings>
      <Selecting AllowRowSelect="True" />
    </ClientSettings>
    <mastertableview commanditemdisplay="TopAndBottom" datakeynames="OldEmpId, NewEmpId, EmpName">
        <commanditemsettings showaddnewrecordbutton="false" />
            <Columns>
                <telerik:GridBoundColumn DataField="OldEmpId" HeaderText="OldEmpId">
                    <HeaderStyle Width="110px"></HeaderStyle>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="NewEmpId" HeaderText="NewEmpId">
                    <HeaderStyle Width="110px"></HeaderStyle>
                </telerik:GridBoundColumn>
                <ItemTemplate>
                    <asp:CheckBox ID="ChkChange" runat="server" />
                </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
    </mastertableview>
</telerik:RadGrid>

The following code is just the code I prefer to use with this issue. It uses datakeynames. But it doesn't build:

protected void RadButtonUpdateSectors_Click(object sender, EventArgs e)
{
    foreach (GridItem item in RadGridEmp.MasterTableView.Items)
    {
        GridDataItem dataitem = (GridDataItem)item;
        TableCell cell = dataitem["GridCheckBoxColumn"];
        CheckBox checkBox = (CheckBox)cell.Controls[0];
        if (checkBox.Checked)
        {
            oldEmpId = dataitem.GetDataKeyValue("OldEmpId").ToString();
            newEmpId = dataitem.GetDataKeyValue("NewEmpId").ToString();
            UpdateEmpId(oldEmpId, newEmpId, connString);
        }
    }
}

Solution

  • Please try with the below code snippet.

    datakeynames="OldEmpId,NewEmpId,EmpName"
    ...............
    ...............
    protected void RadButtonUpdateSectors_Click(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGridEmp.MasterTableView.Items)
        {
            CheckBox ChkChange = item.FindControl("ChkChange") as CheckBox;
            if (ChkChange.Checked)
            {
                string oldEmpId = item.GetDataKeyValue("OldEmpId").ToString();
                string newEmpId = item.GetDataKeyValue("NewEmpId").ToString();
                UpdateEmpId(oldEmpId, newEmpId, connString);
            }
        }
    }
    

    Let me know if any concern.

    Note:- Remove extra space from datakeynames.