RadGrid for ASP.NET AJAX

RadControls for ASP.NET AJAX

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.

    1. 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.

Caution

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

CopyASPX
<telerik:RadGrid 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:

See Also