asp.net|datagrid| Sort | data
Visual Studio. NET provides a "premium" number of DataGrid components for writing WinForm programs (that is, programs running under Windows platforms) than the DataGrid components provided by Microsoft's previous development environments. The "advanced" place is that the DataGrid will be able to arrange the data in ascending and descending order, respectively, without any additional setup or programming. This "advanced" feature provides a lot of convenience for us to program. Figure 01 is the interface in which the DataGrid component in the WinForm program is arranged in ascending order of "EmployeeID":
Figure 01: Using the DataGrid to sort data in a WinForm program
At this point many friends will certainly ask such questions as Visual Studio. NET not only provides a DataGrid component for writing WinForm programs, The DataGrid component is also provided for writing WebForm programs (i.e., asp.net page programs), so is the DataGrid in WebForm as "advanced" as the DataGrid in WinForm? The answer is: Although the DataGrid component provided by Microsoft for WebForm also considers data sorting, it is not as easy as the DataGrid component that is provided for writing WinForm programs to implement data sorting functions. To implement the data ordering of the DataGrid component in the ASP.net page, it is possible to set some properties of the component and add some processing code. This article will discuss the issue in detail.
one. This paperIntermediaryThe design and operation Environment of the program in Shaoxing:
(1). Windows 2000 Advanced Server Edition
(2). Visual Studio. NET Chinese official edition,. NET FrameWork SDK version number 3705
two. Asp. NET page in the DataGrid implementation of data sorting principle:
There are two implementations of the DataGrid data-bound data ordering in the ASP.net page, and the two approaches are similar, as follows:
1. In implementing the DataGrid component data binding in the ASP.net page, setting the DataGrid component Data source uses the Dataview,dataview has a property "Sort". You can sort the data in the DataGrid by using the Sort property to sort and sort the data source (that is, ascending, descending).
2. Create a different dataset for data binding by setting the SQL statement to sort the data in the DataGrid.
Although these two approaches achieve the same functionality, the first approach is simpler, but with limited functionality,
Although the second method is more complex to implement, it can achieve stronger function. But either way, the data binding to the DataGrid is the first thing to accomplish. Here's how to implement the DataGrid data binding first:
Three Asp. NET in the DataGrid component data binding implementation steps:
The data-binding methods for implementing the DataGrid component in ASP.net are basically similar to the methods of data binding in WinForm. The following steps are implemented to implement the DataGrid component data binding in asp.net:
1. Start visual Studio. Net.
2. Select Menu "File" | "new" | "Project", pop-up the "New Project" dialog box.
3. Set the project type to Visual C # project.
4. Set "template" to "asp.net Web application".
5. In the "Position" text box, enter "Http://localhost/dataGridSort". Then click the OK button so that you are in visual Studio. NET creates a named "Datagridsort" folder in the directory where the current project file resides, where the project files for this project are located, and where the other files in the project reside in the directory where the IIS default Web site resides. As shown in Figure 02:
Figure 02: Create a new asp.net Project dialog box
6. Take visual Studio. NET Current window to the WebForm design window and drag a DataGrid component into the WebForm Design window from the Toolbox | Web Components tab, named DataGrid1.
7. Select DataGrid1, click the right mouse button, and "AutoFormat" in the pop-up menu. And in the AutoFormat dialog box, select Professional Type 1 in the Select a scheme column.
8. Set the font size of DataGrid1 to "X-small". The DATAGRID1 in this case is shown in Figure 03:
Figure 03: The DataGrid component style after design
9. Take visual Studio. NET switch to the WebForm Code editing window, which is the edit window for the WebForm1.aspx.cs file.
10. In the WebForm1.aspx.cs file header, replace the code for importing namespaces in WebForm1.aspx.cs with the following code:
Using System; Using System.Collections; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Web; Using System.Web.SessionState; Using System.Web.UI; Using System.Web.UI.WebControls; Using System.Web.UI.HtmlControls; Using System.Data.SqlClient; 11. Add the following code in the Page_Load event code area of the WebForm1.aspx.cs file, the following code is the data binding that implements the DataGrid component: SqlConnection sqlConnection1 = new SqlConnection ("Server = localhost; Database = NorthWind; User ID = sa; Password =; " ) ; Defining a database connection DataSet DataSet1; Create DataSet Object SqlDataAdapter SqlDataAdapter1; SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees, sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection DataSet1 = new DataSet (); SqlDataAdapter1.Fill (DataSet1, "employee"); Populating local dataset Datasets with SqlDataAdapter instances DataView dataView1 = dataset1.tables ["Employee"]. DefaultView; DataGrid1.DataSource = DataView1; Datagrid1.databind (); Implementing Data Binding |
12. After the above steps are correctly implemented, all work on implementing the DataGrid data binding is complete, and if you are using a different type of database, simply modify the definition data connection in the code described in step 11th. Readers should note the implementation of the Chinese header for the DataGrid in the above code. When you click the shortcut key F5, you get the following interface:
Figure 04: The DataGrid Implementation data binding interface in the ASP.net page
Four To sort DataGrid data using DataView:
Here's how to sort data from a DataGrid in a project based on the project described above, which is the steps to sort the DataGrid data based on the above project: DataView
1. The visual Stuido. NET Current window to the WebForm design interface and set the DATAGRID1 component's "allowsorting" property value to "True". Then the DATAGRID1 component's column header has one more hyperlink. And when you click the hyperlink, the string that displays the column header is returned. The DataView is based on the returned string to sort the DataGrid data. Figure 05 is the shape in WebForm after setting the DataGrid1 "AllowSorting" property value to "True":
Figure 05: DataGrid1 After setting the "AllowSorting" property value of "True"
2. Take visual Studio. NET switch to the WebForm1.aspx.cs Code editing window for the current window. and add the following code in the WebForm1.aspx.cs InitializeComponent procedure, the following code is the "SortCommand" event that defines DATAGRID1, which is triggered when a hyperlink to the DataGrid1 column header is clicked:
This. Datagrid1.sortcommand + = new System.Web.UI.WebControls.DataGridSortCommandEventHandler (this. Datagrid1_sortcommand); |
3. Add the following code in the WebForm1.aspx.cs class code area, which defines global variables, and note that the variables defined below are static types:
public static bool Blid = TRUE; To identify whether the DataGrid component has been "ordinal" for ascending order public static bool Bllast = FALSE; To identify whether the DataGrid component has a "last name" in ascending order public static bool Blfirst=false; To identify whether the DataGrid component is in ascending order by name public static bool bltitle= false; To identify whether the DataGrid component is "position" in ascending order public static bool Blbirth =false; To identify whether the DataGrid component has been "birthday" in ascending order |
4. After the Page_Load event processing code in WebForm1.aspx.cs, add the following code, which defines the sort procedure, which enables you to arrange the upgrade order for this column of data based on the selection of different columns:
private void Sort (string sortstring) { SqlConnection sqlConnection1 = new SqlConnection ("Server = localhost; Database = NorthWind; User ID = sa; Password =; " ) ; Defining a database connection DataSet DataSet1; Create DataSet Object SqlDataAdapter SqlDataAdapter1; SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees, sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection DataSet1 = new DataSet (); SqlDataAdapter1.Fill (DataSet1, "employee"); Populating local dataset Datasets with SqlDataAdapter instances DataView dataView1 = dataset1.tables ["Employee"]. DefaultView; Switch (sortstring) { Case "Serial number": if (blid) { Dataview1.sort = "Serial number DESC"; Blid = false; } Else { Dataview1.sort = "Ordinal ASC"; Blid = true; } break; Case "Last Name": if (bllast) { Dataview1.sort = "Surname DESC"; Bllast = false; } Else { Dataview1.sort = "Surname ASC"; Bllast = true; } break; Case "Name": if (Blfirst) { Dataview1.sort = "Name DESC"; Blfirst = false; } Else { Dataview1.sort = "Name ASC"; Blfirst = true; } break; Case "title": if (bltitle) { Dataview1.sort = "Job DESC"; Bltitle = false; } Else { Dataview1.sort = "Job ASC"; Bltitle = true; } break; Case "Birthday": if (Blbirth) { Dataview1.sort = "Birthday DESC"; Blbirth = false; } Else { Dataview1.sort = "Birthday ASC"; Blbirth = true; } break; } DataGrid1.DataSource = DataView1; Datagrid1.databind (); Implementing Data Binding } |
5. After clearing all the code in the Page_Load event-handling code area in WebForm1.aspx.cs, and adding the following code in the Page_Load event-handling code area, the following code determines whether this web page is loaded for the first time, and if it is judged to be "True", The data in the DATAGRID1 is sorted in ascending order by the ordinal column name:
if (IsPostBack = = False) { Sort ("ordinal"); } |
6. After the InitializeComponent process in WebForm1.aspx.cs, add the following code, which is the processing code for the DATAGRID1 SortCommand event:
private void Datagrid1_sortcommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { string scolname = E.sortexpression; Get Column Name Sort (sColName); With this column name and sort by the current sort } |
7. After the above steps are correctly implemented, the first way to implement data sorting in the DataGrid is ASP.net, click the shortcut key F5 can run the program, figure 06 and Figure 07, respectively, in accordance with the "Birthday" to the DataGrid in the ascending, sequential arrangement of the running interface:
Figure 06: The data in the DataGrid is sorted in ascending order with "birthday"
Figure 07: Descending order of data in the DataGrid with "birthdays"
Five To sort DataGrid data by using SQL statements:
Using SQL statements to sort the data in the DataGrid component is a bit more complex, albeit a little more powerful, on the operational steps. The main idea is to form different SQL statements according to different column names of the DataGrid components, so that different dataset instances can be obtained to sort the data in the DataGrid accordingly. The following is done on the basis of the project "sort the DataGrid data using DataView in the ASP.net page", which completes the use of SQL statements to sort the data in the DataGrid.
1. First let's assume that you have successfully completed the above project and can sort the data in the DataGrid using the DataView implementation in asp.net.
2. The visual Stuido. NET is switched to WebForm1.aspx.cs, and the following code is added to the class code area of the WebForm1.aspx.cs file, the following code is an instance of creating a global use:
Public DataSet DataSet1; Public SqlDataAdapter SqlDataAdapter1; |
3. Replace the sort procedure already defined in WebForm1.aspx.cs with the following code, which is to redefine the sort process so that it can use SQL statements to sort the data in the DataGrid:
private void Sort (string sortstring) { SqlConnection sqlConnection1 = new SqlConnection ("Server = localhost; Database = NorthWind; User ID = sa; Password =; " ) ; Switch (sortstring) { Case "Serial number": if (blid) { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees (ordinal) ASC ", sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blid = false; } Else { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by serial number DESC ", sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blid = true; } break; Case "Last Name": if (bllast) { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by surname ASC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Bllast = false; } Else { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by surname DESC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Bllast = true; } break; Case "Name": if (Blfirst) { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees the order by name ASC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blfirst = false; } Else { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by name DESC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blfirst = true; } break; Case "title": if (bltitle) { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by post ASC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Bltitle = false; } Else { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by post DESC ", SqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Bltitle = true; } break; Case "Birthday": if (Blbirth) { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees-Birthday ASC, sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blbirth = false; } Else { SqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter ("Select EmployeeID as serial number, LastName as surname, FirstName as name, Title as title, birthdate as birthday from Employees order by birthday DESC ", sqlConnection1); Initializes a SqlDataAdapter instance with a defined database connection Blbirth = true; } break; } DataSet1 = new DataSet (); SqlDataAdapter1.Fill (DataSet1, "employee"); Populating local dataset Datasets with SqlDataAdapter instances DataGrid1.DataSource = DataSet1; Datagrid1.databind (); Implementing Data Binding } |
4. Save the modification steps above, thus realizing the transformation from DataView to SQL statements to sort the DataGrid data. Click the shortcut key F5 to see the interface shown in Figure 06 and figure 07.
Six. Summary:
Through the introduction of the above, we not only understand, mastered the ASP.net page in the DataGrid in the data sorting two ways, but also should understand and grasp the following:
1. Data binding of the DataGrid component in asp.net.
2. Change the header hint content in the DataGrid component of the ASP.net page.