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

Preserve Grid checkbox column while paging

9 Answers 781 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Madhan
Top achievements
Rank 1
Madhan asked on 30 Aug 2012, 02:13 AM
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();
    }  


9 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Aug 2012, 11:01 AM
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
0
Madhan
Top achievements
Rank 1
answered on 31 Aug 2012, 01:07 AM

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;
  
                }
            }
}

0
Madhan
Top achievements
Rank 1
answered on 31 Aug 2012, 04:06 AM
I got it working now. I had bad data in the productid. Thanks.
0
a
Top achievements
Rank 1
answered on 08 Mar 2016, 05:28 PM

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

0
Viktor Tachev
Telerik team
answered on 09 Mar 2016, 01:49 PM
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
0
a
Top achievements
Rank 1
answered on 09 Mar 2016, 06:30 PM

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 .

0
Viktor Tachev
Telerik team
answered on 10 Mar 2016, 01:31 PM
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
0
PETER
Top achievements
Rank 1
answered on 12 Dec 2016, 01:07 AM
Jayesh thanks for this code. Works great. There is some legacy code under the Grid section in the docs.telerik section that does not work. Could you please remove it (http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/how-to/persisting-checkbox-control-state-in-gridtemplatecolumn-on-rebind)?
0
Eyup
Telerik team
answered on 15 Dec 2016, 06:39 AM
Hello Peter,

I am sending some samples which can prove helpful in this scenario.

Regards,
Eyup
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Madhan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Madhan
Top achievements
Rank 1
a
Top achievements
Rank 1
Viktor Tachev
Telerik team
PETER
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or