The DataGrid is an important control in asp.net, and often we make the DataGrid paginated and sortable, sometimes with the option to be added. These are the methods that are often needed, but they are relatively simple.
Design ideas:
For convenience, we connect the Orders table of the Northwind database for SQL Server 2000 and get a data view of the table from the database. Use the SortCommand event in the DataGrid to implement sorting. Implement the selection with a template column plus a CheckBox control. You can use the paging option in the DataGrid's Property builder or modify the HTML implementation pagination yourself.
Html:
Add a DataGrid, named Dgorder.
A template column is added, and a CheckBox control named CB is placed in the template column. This column is used to implement the selection
Adds a sort expression sortexpression to each column that you want to sort.
Use the dataformatstring of the column to format the column, like dataformatstring= "{0:d}" to display the date format.
Set pagesize= "15" to display 15 rows of data per page, allowpaging= "True" to allow paging.
Private Sub Dgorder_sortcommand (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles Dgorder.sortcommand
Private Sub dgorder_pageindexchanged (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgorder.pageindexchanged
The results of the operation are as follows: (Click on the column headers to sort)
In order to know which records the user chooses, we can use DataGridItem's FindControl to get the checkbox value, we add a button, and then write the following code:
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
The code above RegisterClientScriptBlock adds a Java Script Popup dialog box. (In fact, VB Script dialog box than Java Script dialog more display and control mode, but Netscape browser does not support, you can according to the corresponding project in the program to choose what kind of script).
Summarize:
DataGrid is our common Web control, sometimes we can mix with DataList, by modifying the HTML page, can achieve good page effect. It's just an example of how I wrote the Data Access section (SQL) to the page to make it easier to understand the whole process. In software development, we generally have access to the data part of the data layer, page call data layer to get the data, so logic clear, modify and maintenance are very convenient.
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.