The DataGrid Control establishes a solid bidirectional sorting

Source: Internet
Author: User
We all know that the DataGrid Control has built-in events to sort records. You can click the column title to sort records according to the corresponding column.

Very convenient and simple.

However, it also has a major disadvantage, that is, the paging function of the datagrid proxy can only be sorted in one way, and the sorting information will be lost after the page is changed.

We need to improve the paging event function and use the state package ViewState variable to achieve a stable bidirectional sorting. :)

(1) when loading the page, read the data from the data source and send it to the DataGrid.

Private void page_load (Object obj, EventArgs e)
{
If (! Page. IsPostBack)
{
DataLoad ();
}
}

Private void DataLoad ()
{
String connstr = ConfigurationSettings. etettings ["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 [" sortdirection "]. 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) Add the following options to the property settings of the DataGrid Control;

...
Allowpaging = "true"
Pagesize = 13
Pagerstyle-mode = numericpages
Onpageindexchanged = "changepage"
...

Here, allowpaging = "true" indicates that the paging function is enabled. The size of each page is smaller (pagesize = 13) so that multiple pages are allowed.
Pagerstyle-mode = numericpages: sets the paging mode as a number, and finally specifies the event handler as the changepage method.

Private void changepage (Object obj, DataGridPageChangedEventArgs e)
{
Grid1.CurrentPageIndex = e. NewPageIndex; // the current page is equal to the new page.
DataLoad ();
}

(3) Finally, the sorting event 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 ();
}
<% @ Page Language = "C #" %>
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. oledb" %>

<Script language = "C #" runat = "server">

Private void page_load (Object OBJ, eventargs E)
{
If (! Page. ispostback)
{
Dataload ();
}
}

Private void dataload ()
{
String connstr = configurationsettings. etettings ["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 [" sortdirection "]. 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 the 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>

<Form runat = "server">

<Asp: datagrid id = "grid1" runat = "server"
Alternatingitemstyle-backcolor = "# feeeee"
ItemStyle-BackColor = "# CCCCCC"
AutoGenerateColumns = "false"
Headerstyle-backcolor = "lightyellow"
Font-size = "10pt"
Bordercolor = "#999999"

Allowpaging = "true"
Pagesize = 13
Pagerstyle-mode = numericpages
Onpageindexchanged = "changepage"

AllowSorting = "True"
Onsortcommand = "grid_sort"
>
<Columns>

<Asp: templatecolumn headertext = "no." SortExpression = "id">
<Itemtemplate>
<Span> <% # DataBinder. Eval (Container. DataItem, "id") %> </span>
</Itemtemplate>
</Asp: templatecolumn>

<Asp: templatecolumn headertext = "username" sortexpression = "username">
<Itemtemplate>
<Span> <% # databinder. eval (container. dataitem, "username") %> </span>
</Itemtemplate>
</ASP: templatecolumn>

<Asp: templatecolumn headertext = "phone" sortexpression = "phone">
<Itemtemplate>
<Span> <% # databinder. eval (container. dataitem, "phone") %> </span>
</Itemtemplate>
</ASP: templatecolumn>

<Asp: templatecolumn headertext = "ip address" sortexpression = "ip_address">
<Itemtemplate>
<Span> <% # databinder. eval (container. dataitem, "ip_address") %> </span>
</Itemtemplate>
</ASP: templatecolumn>

</Columns>
</Asp: datagrid>
</Form>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.