Preserve Grid checkbox column while paging

8 posts, 0 answers
  1. Madhan
    Madhan avatar
    14 posts
    Member since:
    Aug 2012

    Posted 29 Aug 2012 Link to this post

    Hi Telerik Team - I have a grid that has gridTemplateColumn checkbox column. so If the user checks some rows and in the second screen if he checks some rows then return backs to previous rows needs to show correct records. I searched in this forum, found code and created with northwind database. But sometimes when I check some items in 4 pages and then returns back not preserving the checkboxes as expected. Can anyone please guide me on this?
    aspx:
      
    <telerik:GridTemplateColumn UniqueName="TemplateColumn" SortExpression="LastName">
                                            <ItemTemplate>
                                  
                                    <asp:checkbox ID="checkColumn" runat="server" />
                                  
              </ItemTemplate>
             </telerik:GridTemplateColumn>
                                              
                                               
                                                 
                                               <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32" 
                                                   HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" 
                                                   UniqueName="OrderID">
                                               </telerik:GridBoundColumn>
                                               <telerik:GridBoundColumn DataField="ProductID" DataType="System.Int32" 
                                                   HeaderText="ProductID" ReadOnly="True" SortExpression="ProductID" 
                                                   UniqueName="ProductID">
                                               </telerik:GridBoundColumn>
                                               <telerik:GridBoundColumn DataField="UnitPrice" DataType="System.Decimal" 
                                                   HeaderText="UnitPrice" SortExpression="UnitPrice" UniqueName="UnitPrice">
                                               </telerik:GridBoundColumn>
                                          
      
                                              
                                               
                                                 
                                            </Columns
      
      
      
    aspx.cs:
      
     ArrayList selectedItems; 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Selected"] = null;
            }  
      
        }
      
        private void RememberSelected()
        {
            selectedItems = new ArrayList();
            int index = -1;
      
           // foreach (GridDataItem row in  RadGrid1.MasterTableView.Items)//RadGrid1.Items)
                foreach (GridDataItem row in RadGrid1.Items)
            {
                index = (int)row.GetDataKeyValue("ProductID");
                bool result = ((CheckBox)row.FindControl("CheckColumn")).Checked;
      
                // Check in the Session   
                if (ViewState["Selected"] != null)
                    selectedItems = (ArrayList)ViewState["Selected"];
                if (result)
                {
                    if (!selectedItems.Contains(index))
                        selectedItems.Add(index);
                }
                else
                    selectedItems.Remove(index);
            }
      
            if (selectedItems != null && selectedItems.Count > 0)
                ViewState["Selected"] = selectedItems;
        }
      
      
        protected void RadGrid1_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
        {
            RememberSelected();
        }
      
        protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (selectedItems != null)
            {
                if (e.Item is GridDataItem)
                 
                {
                    GridDataItem row = (GridDataItem)e.Item;
      
                    int index = (int)row.GetDataKeyValue("ProductID");
      
                    if (selectedItems.Contains(index))
                    {
                        e.Item.Selected = true;
                         
                         
                    }
                }
            }
        }
      
        protected void RadGrid1_SortCommand(object source, GridSortCommandEventArgs e)
        {
            RememberSelected();
        }  


  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 30 Aug 2012 Link to this post

    Hello,

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource"
                AllowPaging="true" PageSize="1" OnPageIndexChanged="RadGrid1_PageIndexChanged"
                OnPreRender="RadGrid1_PreRender">
                <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
                    <Columns>
                        <telerik:GridTemplateColumn UniqueName="TemplateColumn">
                            <ItemTemplate>
                                <asp:CheckBox ID="checkColumn" runat="server" />
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridBoundColumn HeaderText="ID" DataField="ID" UniqueName="ID">
                        </telerik:GridBoundColumn>
                        <telerik:GridEditCommandColumn>
                        </telerik:GridEditCommandColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings>
                </ClientSettings>
            </telerik:RadGrid>
    protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                Session["selectedID"] = null;
            }
        }
     
     
     
     
     
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            dynamic data = new[] {
                    new { ID = 1, Name ="Name1"},
                    new { ID = 2, Name = "Name2"},
                    new { ID = 3, Name = "Name3"}
                };
            RadGrid1.DataSource = data;
        }
        protected void RadGrid1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
        {
            RememberSelected();
        }
     
        private void RememberSelected()
        {
            string strIds = ",";
     
            if (Session["selectedID"] != null)
            {
                strIds =Convert.ToString(Session["selectedID"]);
            }
     
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                CheckBox checkColumn = item.FindControl("checkColumn") as CheckBox;
                int Id =Convert.ToInt32(item.GetDataKeyValue("ID").ToString());
     
                if (checkColumn != null && checkColumn.Checked)
                {
                    strIds += Id.ToString() + ",";
                }
                else
                {
                    strIds = strIds.Replace("," + Id.ToString() + ",",",");
                }
            }
     
            Session["selectedID"] = strIds;
        }
     
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            string strIds = ",";
     
            if (Session["selectedID"] != null)
            {
                strIds = Convert.ToString(Session["selectedID"]);
            }
     
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                CheckBox checkColumn = item.FindControl("checkColumn") as CheckBox;
                int Id = Convert.ToInt32(item.GetDataKeyValue("ID").ToString());
     
                if (checkColumn != null)
                {
                    if (strIds.IndexOf("," + Id.ToString() + ",") >= 0)
                    {
                        checkColumn.Checked = true;
                    }
                    else
                    {
                        checkColumn.Checked = false;
                    }
                }
            }
        }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Madhan
    Madhan avatar
    14 posts
    Member since:
    Aug 2012

    Posted 30 Aug 2012 Link to this post

    Thanks for the help, Jayesh. Instead of storing the checked values in String, I stored them in object. I wrote code myself for that and I have successfully placed the checked rows to Session Object and retrieved data from session object when previous page is clicked. Now, I am having issues with session object that when I select data from different pages and moved to other page it is not storing it properly in session object and retrieving.

    I stepped through the code and found out that Session["selectedItems"] always gets saved last page checked items and the earlier page checked items are not saved in it. Please guide me on this issue. Thanks!

     

    private void RememberSelected()
        {   
    selectedItems = new ArrayList();
                foreach (GridDataItem row in RadGrid1.MasterTableView.Items)
                {
                    var index = (int)row.GetDataKeyValue("ProductID");
                    bool result = ((CheckBox)row.FindControl("CheckColumn")).Checked;
      
                    if (result)
                    {
                        selectedItems.Add(index);
                        Session["selectedItems"] = selectedItems;
      
                    }
                }
    }

  5. Madhan
    Madhan avatar
    14 posts
    Member since:
    Aug 2012

    Posted 30 Aug 2012 Link to this post

    I got it working now. I had bad data in the productid. Thanks.
  6. a
    a avatar
    2 posts
    Member since:
    Mar 2016

    Posted 08 Mar in reply to Jayesh Goyani Link to this post

    Hi Jayesh,

    I did n't find the solution with your post.Actually when i click on paging it hit to Item Command event there it is going to set every checkbox to false.Could you please provide me a brief steps.Thanks for your help

  7. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 09 Mar Link to this post

    Hello,

    if you would like to use CheckBoxesto select some rows in a RadGrid control and persist the selection after postback you can use one of the approaches below.
     


    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  8. a
    a avatar
    2 posts
    Member since:
    Mar 2016

    Posted 09 Mar in reply to Viktor Tachev Link to this post

    Hi,

    What my Question is i have a RadGrid with Header Check box and Item Check Box when i check 1st page 3rd row check box as well as 2nd page 6 th row checkbox selected  i want to know total selected checkbox count and as well as IDs of checkbox i.e(OrderID or anything....).Now iam able to get the values from 1st page checkbox selected value only but i want all the selected checkbox values i.e is First and Second Pages Checked Values .

  9. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 10 Mar Link to this post

    Hello,

    Check out the following code-library that illustrates how you can select the items in all RadGrid pages. You can use similar logic to persist the selected items and get the relevant information from them:



    Regards,
    Viktor Tachev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017