Row Selection in GridView

2 posts, 1 answers
  1. Aarsh
    Aarsh avatar
    192 posts
    Member since:
    Jun 2012

    Posted 16 Apr 2013 Link to this post

    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

  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Apr 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top