ASP.NET DataGrid sort, selection, and pagination

xiaoxiao2021-03-06  58

DataGrid is an important control in the ASP.NET, often we all make DataGrid into a pageable and sankable, sometimes you need to add a selection function. These are all methods that often need to be used, in fact, is relatively simple.

Design ideas:

For convenience, we connect the ORDERS table of the Northwind database of SQL Server 2000, and get the data view of this table from the database. Sort by using DataGrid's SortCommand event. Use a template column to add the Checkbox control to achieve selection. You can use the "paging" option of the property generator of the DataGrid or you can modify the HTML implementation paging.

HTML:

Add a DataGrid, named DGORDER.

Added a template column, a template column places a CHECKBOX control called CB. This column is used to achieve the selection

Sort each column to be sorted with sorted expression sortexpression.

Using the column DataFormatString to format the column, like the DataFormatString = "{0: D}" display date format.

Set up pagesize = "15" to display 15 lines of data per page, sopaging = "true" is allowed to page.

Ield = "Orderid" sortexpression = "OrderID" Headertext = "ID">

After adding the following code: imports system.data.sqlclclient 'Get data view, parameter is the column private function getDV (Byval Strsort As String) AS DataView' Define Database Connection DIM DV AS DATAVIEW DIM CN AS New SqlConnection () TRY 'Initializing connection string cn.connectionstring = "data source = pmserver; initial catalog = northwind; Persist security info = false; user ID = sa; password = sa;" cn.open () ()' Data DIM from Northwind to ORDERS table ADP As Sqldataadapter = New SqldataAdapter ("Select * from Orders", CN) DIM DS AS NEW DATASET () ADP.FILL (DS) 'Get Data View DV = DS.TABLES (0) .defaultView Catch EX AS Exception #if Debug The session ("error") = ex. TString () Response.Redirect ("../ error.aspx") 'Jump program Public Error handling page #END if finally' Close connection CN.Close () end ' Sort dv.Sort = strSort Return dv End Function Private Sub Page_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then ViewState ( "strSort") = "orderid" dgOrder.DataSource = GetDV ("strste"). Tostring ()) DGORDER.DATAB ind () End If End Sub 'sort Private Sub dgOrder_SortCommand (ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgOrder.SortCommand dgOrder.CurrentPageIndex = 0' obtained sorted column ViewState ( "strSort" ) = e.SortExpression.ToString () dgOrder.DataSource = GetDv (ViewState ( "strSort"). ToString ()) dgOrder.DataBind () End Sub 'tab Private Sub dgOrder_PageIndexChanged (ByVal source As Object, ByVal e As System.Web .Ui.webcontrols.DataGridPageChangeDeventargs) Handles Dgorder.pageIndexchanged '

Get paging page number DGORDER.CURRENTPAGEINDEX = E.NewpageIndex Dgorder.DataSource = getDV (ViewState ("strs"). TOSTRING ()) DGORDER.DATABIND () END SUB running results As shown below :( Click the header to sort In order to know which records are selected, we can use the DataGridItem's FindControl to get Checkbox's value, let's add a button, write the following code: Private sub button1_click (Byval e AS System.EventArgs) Handles Button1.Click Dim item As DataGridItem Dim StrScript As String StrScript = "