This is a migrated thread and some comments may be shown as answers.

checkbox to select row

1 Answer 15 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joslyn
Top achievements
Rank 1
Joslyn asked on 11 Nov 2013, 01:53 PM
Hi.
 I know we can select row using allowrowselect to true, but i want to know if by just using checkbox is it possible to select rows?

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 11 Nov 2013, 01:58 PM
Hi Joslyn,

Please try the following code snippet to select rows using CheckBox.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="true">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="ToggleRowSelection"
                        AutoPostBack="True" />
                </ItemTemplate>
                <HeaderTemplate>
                    <asp:CheckBox ID="headerChkbox" runat="server" OnCheckedChanged="ToggleSelectedState"
                        AutoPostBack="True" />
                </HeaderTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void ToggleRowSelection(object sender, EventArgs e)
{
    ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
    bool checkHeader = true;
    foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
    {
        if (!(dataItem.FindControl("CheckBox1") as CheckBox).Checked)
        {
            checkHeader = false;
            break;
        }
    }
    GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem;
    (headerItem.FindControl("headerChkbox") as CheckBox).Checked = checkHeader;
}
protected void ToggleSelectedState(object sender, EventArgs e)
{
    CheckBox headerCheckBox = (sender as CheckBox);
    foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
    {
        (dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
        dataItem.Selected = headerCheckBox.Checked;
    }
}

Thanks,
Princy
Tags
Grid
Asked by
Joslyn
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or