DataGrid is a very important control in ASP.NET. It is convenient to make us edit, sort the function; however, the sorting function is default ascending (ASC), can you make DataGrid simultaneously arrange? This article will give you a better solution.
The following example will tell you how to dynamically add the sortexpression and sortdirection properties to DataGrid and arrange the data in DataGIRD in these two attributes via DataView. The sortexpression attribute of the DataGrid used in this example only needs to be set in the SortCommand event (and usually sorted), the DataGrid's SortExpression property saves the last field name used to sort, and the DataGrid's SortDirection property saves the last used. Sort field arrangement ("ASC" or "DESC")
page design:
1. Add a DataGrid on the page;
2. Set the instalowsorting property of the DataGrid to True;
3. Set the autogeneratecolumns attribute to false;
4. Add the fields to be bound to the DataGrid and set their properties according to the table below. The sortexpression property is consistent with the field name in the data table in the database.
I will illustrate this example using Employees in the Northwind database.
DataTextField
HEADER TEXT
Sort Expression
Employeeid
EMPLOYEE ID
Employeeid
Lastname
Last Name
Lastname
Firstname
First Name
Firstname
Title
Title
Title
CITY
CITY
CITY
COUNTRY
COUNTRY
COUNTRY
Program code design:
1. Add dynamic properties to DataGrid to DataGrid when loading the DataGrid; SortExpression and Sortdirection;
2. Assign a value to the sortexpression and sortdirection of the specified field, initialize the sort field and sorting mode of the DataGrid when loading;
3. Get the DataSet and DataView objects. Set the sort properties of the DataView as dynamic properties sortexpression and sortdirection connection strings;
4. Bind DataGrid with DataView;
5. When the head is clicked, trigger the SortCommand event of DataGrid;
6. Get the sortexpression attribute passed by the SortCommand event and compare with the DataGrid's sortexpression attribute;
7. If you find equal
a) Check the sortdirection attribute first;
b) if sortdirection attribute equal to "ASC" THEN
i. Set the value of the SortDirection property equal to "DESC"
c) ELSE
i. Set the value of the SortDirection property equal to "ASC"
d) endiff
8. Repeat step 3
Note: When you run the following code and click on the same column twice or more, the arrangement of this column will automatically arrange in the reselde sequence of the existing arrangement.
The following code will give you how to use DataGrid's dynamic properties and DataView objects.
C # cote:
Private void Page_load (Object Sender, System.Eventargs E) {
/ / Place the user code here to initialize the page
IF (! page.ispostback)
{
IF (DataGrid1.attributes ["sortexpression"] == null)
{
DataGrid1.attributes ["sortexpression"] = "lastname";
DataGrid1.attributes ["sortdirection"] = "ASC";
}
Bindgrid ();
}
}
Private void bindgrid ()
{
SqlConnection conn = new sqlconnection ("server = localhost; uid = sa; pwd = sa; dataBase = northwind");
Cn.open ();
Sqldataadapter cmd = new SqldataAdapter ("Select * from Employees", CONN);
DataSet DS = New DataSet ();
Cmd.Fill (DS, "Employees");
DataView DV = New DataView ();
DV = ds.tables [0] .defaultView;
String sortexpression = dataGrid1.attributes ["sortexpression"];
String Sortdirection = DataGrid1.attributes ["sortdirection"];
Dv.Sort = sortexpression "" sortdirection;
DataGrid1.datasource = DV;
DataGrid1.databind ();
}
Private void DataGrid1_sortcommand (Object Source, System.Web.ui.WebControls.DataGridsortCommandeventArgs E)
{
String sortexpression = e.sortexpression.toString ();
String sortdirection = "ASC";
IF (sortexpression == DataGrid1.attributes ["sortexpression"])
{
Sortdirection = (DataGrid1.attributes ["sortdirection"]. TOSTRING () == Sortdirection? "DESC": "ASC");
}
DataGrid1.attributes ["sortexpression"] = sortexpression;
DataGrid1.attributes ["sortdirection"] = sortdirection;
Bindgrid ();
}
VB.Net Code:
Private Sub Page_Load (Byvale AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load
'Put User Code to Initialize the page hereif not page.ispostback then
If DataGrid1.attributes ("sortexpression") is nothing then
DataGrid1.attributes ("sortexpression") = "lastname"
DataGrid1.attributes ("sortdirection") = "ASC"
END IF
BindDataGrid ()
END IF
End Sub
Private sub binddatagrid ()
DIM CN As SqlConnection
DIM CMDSELECT As SqlCommand
CN = New SqlConnection ("Server = Amandrek; UID = SA; PWD =; Database = northwind")
DIM strsql as string = "select * from employees"
DIM DA AS New SqldataAdapter (strsql, cn)
DIM DS AS New DataSet ()
Da.fill (DS, "Table1")
DIM DV AS DATAVIEW = DS.TABLES (0) .defaultView
DIM Sortexpression as string = dataGrid1.attributes ("sortexpression")
DIM Sortdirection As String = DataGrid1.attributes ("Sortdirection")
DV.Sort = sortexpression " Sortdirection
DataGrid1.datasource = DV
DataGrid1.databind ()
cn.close ()
End Sub
Private sub DataGrid1_sortcommand (Byval Source As Object, Byval E as System.Web.ui.WebControls.DataGridsortCommandeventArgs) Handles DataGrid1.SortCommand
DIM sortexpression as string = e.sortexpression
DIM Sortdirection As String = "ASC"
IF sortexpression.equals ("sortexpression"). Tostring ()) THEN
If DataGrid1.attributes ("sortdirection"). Tostring (). StartSwith ("ASC") THEN
Sortdirection = "desc"
Else
Sortdirection = "ASC"
END IF
END IF
DataGrid1.attributes ("sortexpression") = sortexpression
DataGrid1.attributes ("sortdirection") = Sortdirection
BindDataGrid ()
End Sub