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

Retain chkBx value on Paging

1 Answer 55 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Amanda
Top achievements
Rank 1
Amanda asked on 20 Jul 2011, 04:38 PM
I have a column of check boxes that when selected is inserting a row into a database.  I also have Paging=true in the radGrid as this application will continue to grow.  If I check chkBx on Page 1 and then go over to Page 2 and select someone else - when I hit submit - how do I get it to check all the pages?

Thanks,
Amanda

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Oct 2012, 10:25 AM
Hi,

RadGrid loses its current selection when the data is sorted, a new group or filter is added, or when the current page changes. In order to persist the changes, try the following code.
C#:
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    ArrayList selectedItems;
    if (Session["selectedItems"] == null)
    {
        selectedItems = new ArrayList();
    }
    else
    {
        selectedItems = (ArrayList)Session["selectedItems"];
    }
    if (e.CommandName == RadGrid.SelectCommandName && e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string customerID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString();
        selectedItems.Add(customerID);
        Session["selectedItems"] = selectedItems;
    }
    if (e.CommandName == RadGrid.DeselectCommandName && e.Item is GridDataItem)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string customerID = dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString();
        selectedItems.Remove(customerID);
        Session["selectedItems"] = selectedItems;
    }
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (Session["selectedItems"] != null)
    {
        ArrayList selectedItems = (ArrayList)Session["selectedItems"];
        Int16 stackIndex;
        for (stackIndex = 0; stackIndex <= selectedItems.Count - 1; stackIndex++)
        {
            string curItem = selectedItems[stackIndex].ToString();
            foreach (GridItem item in RadGrid1.MasterTableView.Items)
            {
                if (item is GridDataItem)
                {
                    GridDataItem dataItem = (GridDataItem)item;
                    if (curItem.Equals(dataItem.OwnerTableView.DataKeyValues[dataItem.ItemIndex]["CustomerID"].ToString()))
                    {
                        dataItem.Selected = true;
                        break;
                    }
                }
            }
        }
    }
}

Thanks,
Princy.
       
Tags
General Discussions
Asked by
Amanda
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or