Establish a solid bidirectional sort (ASP.NET) to the DataGrid control
Everyone knows that the DataGrid control has built-in events to sort the records. Users can click on the column headers, they will be sorted according to the corresponding columns.
Very convenient and simplified.
However, it also has a lot of shortcomings, is the paging function of the DataGrid agent, only one-way sorting, and after the page, the ordering information will be lost. So I
We need to improve the paging event function, using the status pack viewState variable to achieve a stable two-way sort. :)
(1) First on page LOAD. To read the data in the data source, and send it to the dataGrid
Private void Page_load (Object Obj, Eventargs E) {if (! page.ispostback) {dataAload ();}}
Private void DataLoad () {string connStr = configurationSettings.appsettings ["connectionstring"]; OLEDBConnection conn = new oledbconnection (connStr); DataSet DS = New DataSet ();
String SQL;
IF (ViewState ["sorting"] == null) {sql = "select * from userinfo"; response.write (sql);} else {sql = "select * from userinfo order by" viewState ["sorting"]. Tostring () "
" ViewState]. TOSTRING (); response.write (sql);
OLEDBDataAdapter Da = New OLEDBDataAdapter (SQL, CONN); DA.FILL (DS); DataView DV = New DataView (ds.tables [0]); grid1.datasource = DV; grid1.databind ();}
(2) The following option is added to the attribute setting of the DataGrid control;
... allowpaging = "true" Pagesize = 13 Pagersty-mode = numericpages onpageIndexchanged = "changepage" ...
Ampoly, Paging function, indicating the size of the paging, and we set a small number (PageSize = 13), so that you multi-page. PagersTyle-mode = NuMericPages is set to numbers, finally designated The handler of the event is a ChangePage method.
Private Void ChangePage (Object Obj, DataGridPageChangeDeventargs E) {grid1.currentpageindex = E.NewpageIndex; // The current page is equal to new page DataLoad ();}
(3) Finally, the event sort function: private void grid_sort (Object obj, DataGridSortCommandEventArgs e) {ViewState.Add ( "sorting", e.SortExpression); if (ViewState [ "sortdirection"] == null) ViewState.Add ( "SortDirection", "ASC"); Else {IF (ViewState ["SortDirection"]. TOSTRING () == "ASC") ViewState ["sortdirection"] = "desc"; else ViewState ["sortdirection] =" ASC "} DATALOAD ();} The entire full program is:
<% @ page language = "c #"%> <% @ Import namespace = "system.data"%> <% @ import namespace = "system.data.oledb"%>
Private void Page_load (Object Obj, Eventargs E) {if (! page.ispostback) {dataAload ();}}
Private void DataLoad () {string connStr = configurationSettings.appsettings ["connectionstring"]; OLEDBConnection conn = new oledbconnection (connStr); DataSet DS = New DataSet ();
String SQL;
IF (ViewState ["sorting"] == null) {sql = "select * from userinfo"; response.write (sql);} else {sql = "select * from userinfo order by" viewState ["sorting"]. Tostring () "
" ViewState]. TOSTRING (); response.write (sql);
OLEDBDataAdapter Da = New OLEDBDataAdapter (SQL, CONN); DA.FILL (DS); DataView DV = New DataView (ds.tables [0]); grid1.datasource = DV; grid1.databind ();}
Private Void ChangePage (Object Obj, DataGridPageChangeDeventargs E) {grid1.currentpageindex = E.NewpageIndex; // The current page is equal to new page DataLoad ();}
private void grid_sort (Object obj, DataGridSortCommandEventArgs e) {ViewState.Add ( "sorting", e.SortExpression); if (ViewState [ "sortdirection"] == null) ViewState.Add ( "sortdirection", "ASC"); else {If (ViewState ["sortdirection"]. TOSTRING () == "ASC") ViewState ["sortdirection"] = "desc"; Else ViewState ["sortdirection" = "ASC";} DataLoad (); Script>