Telerik gridview checkbox needs to persistant.

4 posts, 1 answers
  1. Siva
    Siva avatar
    9 posts
    Member since:
    Oct 2012

    Posted 25 Jul 2013 Link to this post

    Hi admin and all,

    I have a rad grid with a check box in each row. whenever I change page or scroll, the checkboxes loose their selected state. How can I handle that?. Small additional information i am using virtual scrolling and virtual paging in radgrid.

    This is an urgent requirement for me. Give reply as soon as possible.
    Thanks and Regards,
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Jul 2013 Link to this post

    Hi Siva,

    Please try the below example code snippet to get the checkbox item to keep its state on paging or scrolling.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server"  AllowPaging="true"
        AutoGenerateColumns="false" OnPreRender="RadGrid1_PreRender">
        <MasterTableView DataKeyNames="OrderID">
            <Columns>
                <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="Name">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" CausesValidation="false"
                            OnCheckedChanged="CheckBox1_CheckedChanged" />
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings Scrolling-EnableVirtualScrollPaging="true" Scrolling-AllowScroll="true">
        </ClientSettings>
    </telerik:RadGrid>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><%--To display the rows checked--%>

    C#:
    public string _SelectedID
    {
        set
        {
            ViewState["SelectedID"] = value;
        }
        get
        {
            if (ViewState["SelectedID"] != null)
            {
                return Convert.ToString(ViewState["SelectedID"]);
            }
            else
            {
                return ",0,";
            }
        } 
    }
          
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    { 
        foreach (GridDataItem item in RadGrid1.Items)
        {
            if (_SelectedID.Contains("," + Convert.ToInt32(item.GetDataKeyValue("OrderID")).ToString() + ","))
            {
                ((CheckBox)item.FindControl("CheckBox1")).Checked = true;
            }
        }
      
    }
            
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox CheckBox1 = sender as CheckBox;
        GridDataItem item = (GridDataItem)CheckBox1.NamingContainer;
        int ID = Convert.ToInt32(item.GetDataKeyValue("OrderID"));
        if (CheckBox1.Checked)
        {
            _SelectedID += ID.ToString() + ",";
            Label1.Text = _SelectedID.ToString();//To display the rows checked Using DatakeyValue of a column
        }
        else
        {
            _SelectedID += _SelectedID.Replace(ID.ToString() + ",", "");
        }
    }

    Hope this helps,let me know if any concern.

    Thanks,
    Princy
  3. Siva
    Siva avatar
    9 posts
    Member since:
    Oct 2012

    Posted 25 Jul 2013 Link to this post

    Thank You Sir,
  4. Rod
    Rod avatar
    1 posts
    Member since:
    Oct 2013

    Posted 16 Jan 2014 Link to this post

    I believe the last line in Princy's solution should read "=" instead of "+="  - thanks Princy, this really helped!

    _SelectedID = _SelectedID.Replace(ID.ToString() + ",", "");
Back to Top