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

Row Selection in GridView

1 Answer 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aarsh
Top achievements
Rank 1
Aarsh asked on 16 Apr 2013, 08:02 PM
I have followed :

partially. And it seemed to be working as expected but on thing, I just wanted one row to be selected at a time.

aspx

GridView

<telerik:RadGrid ID="gv" runat="server" AllowSorting="true" AllowFilteringByColumn="true"
       OnItemCommand="ItemCommand" OnItemDataBound="ItemDataBound"
       AllowAutomaticUpdates="true" AllowMultiRowSelection="false" OnInsertCommand="InsertCommand"
       AllowAutomaticInserts="true" OnUpdateCommand="ItemUpdated" ActiveItemStyle-CssClass="gv_ActiveItem"
       OnNeedDataSource="NeedDataSource">


CheckBox Item Template

<telerik:GridTemplateColumn UniqueName="_SelectCommandColumn" AllowFiltering="false"
                    HeaderText="Select" HeaderAbbr="Check">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbSelectCategory" runat="server" OnCheckedChanged="cbSelectCategory_CheckedChanged"
                            AutoPostBack="true" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>

.cs

protected void cbSelectCategory_CheckedChanged(object sender, EventArgs e)
        {
            ((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
            //getting the id of the selected check-box
        }

the code works well and the style also gets applied to the row containing the checkbox selected. but the only problem is, the checkbox of the row previously selected, remains checked, I need to uncheck it anyway

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Apr 2013, 03:29 AM
Hi,

Please try the following code snippet to check the only one CheckBox at a time.

C#:
protected void cbSelectCategory_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk=(CheckBox)sender;
    GridDataItem itm = (GridDataItem)chk.NamingContainer;
    itm.Selected = true;
    int crrntindex = itm.ItemIndex;
    if (chk.Checked == true)
    {
        foreach (GridDataItem row in gv.MasterTableView.Items)
        {
            CheckBox chk1 = (CheckBox)row.FindControl("cbSelectCategory");
            int index = row.ItemIndex;
            if (crrntindex == index)
                continue;
            if (chk1.Checked == true)
            {
                chk1.Checked = false;
            }
        }
    }
}

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