This behavior is not supported out-of-the-box by our control. However, it is achievable through a custom implementation as follows:
- Store the primary key values for the selected rows in a Session object on Select command execution and remove it on Deselect;
- On sort/page/filter/group command, traverse the grid rows and set those with primary keys present in the Session object with Selected = True;
- To safely apply the selection for the marked grid rows, hook the PreRender event of the grid and check whether the Session object holding the primary fields for the selected records is not empty.
 |
Note: The example will work when you select/deselect rows using the Select/Deselect button columns in the grid. |
| ASPX/ASCX |
Copy Code |
|
<rad:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" runat="server" Width="97%" PageSize="15" AllowSorting="True" AllowMultiRowSelection="True" Skin="Outlook" AllowPaging="True" ShowGroupPanel="True" AutoGenerateColumns="False" AllowFilteringByColumn="true" EnableAJAX="true" ShowStatusBar="true" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender"> <PagerStyle Mode="NextPrevAndNumeric" /> <MasterTableView Width="100%" DataKeyNames="CustomerID" AllowFilteringByColumn="true"> <Columns> <rad:GridBoundColumn SortExpression="Country" HeaderText="Country" HeaderButtonType="TextButton" DataField="Country"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="City" HeaderText="City" HeaderButtonType="TextButton" DataField="City"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="CompanyName" HeaderText="Company Name" HeaderButtonType="TextButton" DataField="CompanyName"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton" DataField="ContactName"> </rad:GridBoundColumn> <rad:GridBoundColumn SortExpression="ContactTitle" HeaderText="Contact Title" HeaderButtonType="TextButton" DataField="ContactTitle"> </rad:GridBoundColumn> <rad:GridButtonColumn UniqueName="SelectColumn" CommandName="Select" Text="Select" /> <rad:GridButtonColumn UniqueName="DeselectColumn" CommandName="Deselect" Text="Deselect" /> </Columns> </MasterTableView> <ClientSettings AllowDragToGroup="True" /> </rad:RadGrid> <br /> <asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb" SelectCommand="SELECT * FROM Customers" runat="server"></asp:AccessDataSource> |
And in the code-behind:
| VB.NET |
Copy Code |
|
Dim selectedItems As ArrayList
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs) Handles RadGrid1.ItemCommand If Session("selectedItems") Is Nothing Then selectedItems = New ArrayList Else selectedItems = CType(Session("selectedItems"), ArrayList) End If
If e.CommandName = RadGrid.SelectCommandName AndAlso TypeOf e.Item Is GridDataItem Then Dim dataItem As GridDataItem = CType(e.Item, GridDataItem) Dim customerID As String = dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("CustomerID").ToString selectedItems.Add(customerID) Session("selectedItems") = selectedItems End If
If e.CommandName = RadGrid.DeselectCommandName AndAlso TypeOf e.Item Is GridDataItem Then Dim dataItem As GridDataItem = CType(e.Item, GridDataItem) Dim customerID As String = dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("CustomerID").ToString selectedItems.Remove(customerID) Session("selectedItems") = selectedItems End If End Sub
Protected Sub RadGrid1_PreRender(ByVal sender As Object, ByVal e As EventArgs) Handles RadGrid1.PreRender If Not (Session("selectedItems") Is Nothing) Then selectedItems = CType(Session("selectedItems"), ArrayList)
Dim stackIndex As Int16
For stackIndex = 0 To selectedItems.Count - 1 Dim curItem As String = selectedItems(stackIndex).ToString
For Each item As GridItem In RadGrid1.MasterTableView.Items If TypeOf item Is GridDataItem Then Dim dataItem As GridDataItem = CType(item, GridDataItem) If curItem.Equals(dataItem.OwnerTableView.DataKeyValues(dataItem.ItemIndex)("CustomerID").ToString()) Then dataItem.Selected = True End If End If Next Next End If End Sub |
| C# |
Copy Code |
|
ArrayList selectedItems;
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) { 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) { 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; } } } } } } |
See Also