New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Persisting the Selected Rows Server-side on Sorting/Paging/Filtering/Grouping

RadGrid loses its current selection when the data is sorted, a new group or filter is added, or when the current page changes. If you want the selection to persist across these events, you can do the following:

  1. Add an ItemCommand event handler to the grid.

    1. When a "Select" command occurs, store the key values for the selected row in a Session variable.

    2. When a "Deselect" command occurs, remove the key values from the Session variable.

  2. Add a PreRender event handler to the grid.

    • In the pre-render event of the grid, traverse the rows of the grid and compare their key values to the values saved in the Session variable. Whenever you find a match, select the row.

This example only works if you have not enabled client-side selection (that is, you are selecting rows using Command buttons).

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Width="97%"
  AllowPaging="True" PageSize="15" AllowSorting="True" AllowMultiRowSelection="True"
  ShowGroupPanel="True" AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand"
  OnPreRender="RadGrid1_PreRender">
  <PagerStyle Mode="NextPrevAndNumeric" />
  <MasterTableView Width="100%" DataKeyNames="CustomerID" AllowFilteringByColumn="true">
    <Columns>
      <telerik:GridButtonColumn UniqueName="SelectColumn" CommandName="Select" Text="Select" />
      <telerik:GridButtonColumn UniqueName="DeselectColumn" CommandName="Deselect" Text="Deselect" />
    </Columns>
  </MasterTableView>
  <ClientSettings AllowDragToGroup="True" />
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
      SelectCommand="SELECT TOP 10 [CustomerID], [ContactName], [ContactTitle], [Address] FROM [Customers]">
</asp:SqlDataSource>

In the code-behind, the ItemCommand handler saves the key values, and the PreRender handler uses them to restore the selection:

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;
                    }
                }
            }
        }
    }
}
Not finding the help you need?
Contact Support