GridView Automatic Ordering Sample sharing _ Practical Tips

Source: Internet
Author: User

Examples are as follows: Front desk

Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:gridview id= "GridView1" runat= "Server" autogeneratecolumns= "False" cellpadding= "4" forecolor= "#333333" Gridlines= "None" allowsorting= "True" onsorting= "gridview1_sorting" >
<footerstyle backcolor= "#507CD1" font-bold= "True" forecolor= "white"/>
<rowstyle backcolor= "#EFF3FB"/>
<Columns>
<asp:boundfield datafield= "id" headertext= "id" sortexpression= "id"/>
<asp:boundfield datafield= "name" headertext= "name" sortexpression= "name"/>
<asp:boundfield datafield= ' age ' headertext= ' age ' sortexpression= ' age '/>
</Columns>
<pagerstyle backcolor= "#2461BF" forecolor= "white" horizontalalign= "Center"/>
<selectedrowstyle backcolor= "#D1DDF1" font-bold= "True" forecolor= "#333333"/>
<editrowstyle backcolor= "#2461BF"/>
<alternatingrowstyle backcolor= "White"/>
</asp:GridView>
</div>
</form>
</body>

Attention point of the front desk:
You need to enable allowsorting for the GridView, set the Onsorting event, and set the SortExpression property for the column that needs to be sorted.

Background

Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;

public partial class _default:system.web.ui.page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Set initial sort parameter values

Incorrect property setting method: SortExpression, SortDirection are GridView read-only properties and cannot be directly assigned.
This. Gridview1.sortexpression = "id";
This. Gridview1.sortdirection = "ASC";

Correct method of setting property
This. GRIDVIEW1.ATTRIBUTES.ADD ("SortExpression", "id");
This. GRIDVIEW1.ATTRIBUTES.ADD ("SortDirection", "ASC");

Binding data Source to GridView
This. Bindgridview ();
}
}

<summary>
GridView Sort Events
</summary>
protected void Gridview1_sorting (object sender, GridViewSortEventArgs e)
{
Getting sorted data columns from event arguments
String sortexpression = E.sortexpression.tostring ();

Assume that the sort direction is "sequential"
String sortdirection = "ASC";

"ASC" is compared to the sort direction obtained by the event parameter to modify the GridView sort Direction parameter
if (SortExpression = = this. gridview1.attributes["SortExpression"])
{
Get the next sort status
SortDirection = (this. gridview1.attributes["SortDirection"]. ToString () = = SortDirection? "DESC": "ASC");
}

Reset GridView Sort data columns and sort direction
This. gridview1.attributes["SortExpression"] = SortExpression;
This. gridview1.attributes["SortDirection"] = SortDirection;

This. Bindgridview ();
}

<summary>
Bind to GridView
</summary>
private void Bindgridview ()
{
Get the GridView Sort data column and sort direction
String SortExpression = this. gridview1.attributes["SortExpression"];
String sortdirection = this. gridview1.attributes["SortDirection"];

Invoking Business Data acquisition methods
DataTable dtbind = This.getdb ();

The default data view to display based on the GridView Sort data columns and sort direction settings
if (!string. IsNullOrEmpty (SortExpression)) && (!string. IsNullOrEmpty (SortDirection))
{
DtBind.DefaultView.Sort = string. Format ("{0} {1}", SortExpression, SortDirection);
}

The GridView binds and displays the data
This. Gridview1.datasource = Dtbind;
This. Gridview1.databind ();
}

<summary>
Ways to get data sources
</summary>
<returns> Data Source </returns>
Private DataTable Getdb ()
{
DataTable dt = new DataTable ();

Dt. Columns.Add ("id");
Dt. Columns.Add ("name");
Dt. Columns.Add ("Age");

Dt. Rows.Add (new object[] {"000001", "Hekui", "26"});
Dt. Rows.Add (new object[] {"000002", "Zhangyu", "26"});
Dt. Rows.Add (new object[] {"000003", "Zhukundian", "27"});
Dt. Rows.Add (new object[] {"000004", "Liyang", "25"});
Dt. Rows.Add (new object[] {"000005", "Caili", "27"});

return DT;
}
}

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.