Many customers are facing such scenes, and they hope to retain the election status of the end user after the application of sorting or filtering. Under normal circumstances, when we choose any line, we apply or filter to cause the selection state after returning. This blog will discuss how we do it to keep the selection state after sorting and filtering.
Step1: willGridViewBind to a data table
First of all, we need to bind GridView to a data table, such as the Categories table from the Northwind database. Since we are using the server selection, we needAutoGenerateSelectButtonProperties are set to “true”, and then “ClientSelectionMode“Properties are set to” None “. Otherwise, we will have two options: client and server.
In addition, we need to set upAllowSortingandShowFilterProperties are “true” for allowinggridviewPerform sort or filtering. The following is the source code of the .aspx page:
<wijmo:C1GridView ID="C1GridView1" runat="server" AllowSorting="True" ClientSelectionMode="None" AutogenerateColumns="False" AutoGenerateSelectButton="True" DataKeyNames="CategoryID" DataSourceID="AccessDataSource1" ShowFooter="False" ShowFilter="True"> <Columns> <wijmo:C1BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True" SortExpression="CategoryID"> </wijmo:C1BoundField> <wijmo:C1BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName"> </wijmo:C1BoundField> <wijmo:C1BoundField DataField="Description" HeaderText="Description" SortExpression="Description"> </wijmo:C1BoundField> <wijmo:C1BoundField DataField="Picture" HeaderText="Picture" SortExpression="Picture"> </wijmo:C1BoundField> <wijmo:C1BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName"> </wijmo:C1BoundField> </Columns> </wijmo:C1GridView> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/C1NWind.mdb" SelectCommand="SELECT * FROM [Categories]"> </asp:AccessDataSource>
Step2:Save the selected line
We need to be in one
TheViewStateobject saves the selected data key value so that we can use it to set the selection again. So we need to handleSelectedIndexChangedevent. The code sheet used in this incident is as follows:
Protected Sub C1GridView1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles C1GridView1.SelectedIndexChanged 'Save the data key value of the selected data row If (Not C1GridView1.SelectedIndex = -1) Then ViewState("SelectedValue") = C1GridView1.SelectedValue End If End Sub
Step3: Reset the selected line index
We need to be completed before sorting or filtering, and re -executing the selection action.gridviewSelectedIndexProperties. This job can be hereSortingor
TheFilteringIncident is completed through the following code films:
Protected Sub C1GridView1_Sorting(sender As Object, e As C1.Web.Wijmo.Controls.C1GridView.C1GridViewSortEventArgs) Handles C1GridView1.Sorting 'Reset selection index C1GridView1.SelectedIndex = -1 End Sub Protected Sub C1GridView1_Filtering(sender As Object, e As C1.Web.Wijmo.Controls.C1GridView.C1GridViewFilterEventArgs) Handles C1GridView1.Filtering 'Reset selection index C1GridView1.SelectedIndex = -1 End Sub
Step4: Re -selected the line
gridviewwill be re -binded when returning (due to the execution of sorting or filtering), and we need to deal withDataBoundEvent to redesigned the selection. Here, we should check whether the original selected row is visible, and then re -select it through the ViewState object. The code sheet is shown below:
Protected Sub C1GridView1_DataBound(sender As Object, e As System.EventArgs) Handles C1GridView1.DataBound Dim Row As C1GridViewRow Dim SelectedValue As String = ViewState("SelectedValue") If SelectedValue Is Nothing Then Return End If 'Check whether the selected row is visible and select it again. For Each Row In C1GridView1.Rows Dim KeyValue As String = C1GridView1.DataKeys(Row.RowIndex).Value If (KeyValue = SelectedValue) Then C1GridView1.SelectedIndex = Row.RowIndex End If Next End Sub
Please refer to the complete example in the attachment.
wijmo download, please enterStudio for asp.net wijmo 2012 V1 is officially released (2012.03.22 update)!
Reprinted: https://www.cnblogs.com/powertoolteam/archive/2012/04/26/2471028.html