DataGrid sort, pagination, select

xiaoxiao2021-03-06  95

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.

Whole HTML page code:

The background class adds the following code:

Imports system.data.sqlclient

'Get data view, the parameter is the column to be sorted

Private function getDV (Byval Strsort As String) AS DataView

'Define database connections

DIM DV AS DATAVIEW

DIM CN As New SqlConnection ()

Try

'Initializing the connection string

Cn.connectionstring = "data source = pmserver; initial catalog = northwind; Persist security info = false; user ID = sa; password = sa;" cn.open ()

'Get data from Northwind to the ORDERS table

DIM 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 exception

#If debug the

Session ("error") = ex.toString ()

Response.Redirect ("../ error.aspx") 'Jumping Program Public Error Processing Page

#End IF

Finally

'Close connection

Cn.close ()

END TRY

'Sort

Dv.sort = strsort

Return DV

END FUNCTION

Private Sub Page_Load (Byvale AS System.Object, Byval E AS System.Eventargs) Handles MyBase.LOAD

IF not ispostback.

ViewState ("strsort") = "OrderID"

DGORDER.DataSource = GetDV (ViewState ("strsort"). TOSTRING ())

Dgorder.DATABIND ()

END IF

End Sub

'Sort

Private Sub Dgorder_sortCommand (Byval E as System.Web.ui.WebControls.DataGridsortCommandeventArgs) Handles Dgorder.SortCommand

Dgorder.currentPageIndex = 0

'Get the column

ViewState ("strsort") = E.Sortexpression.toString ()

DGORDER.DataSource = GetDV (ViewState ("strsort"). TOSTRING ())

Dgorder.DATABIND ()

End Sub

Page

Private Sub Dgorder_pageIndexchanged (Byval E AS ISTEM.WEB.UI.WEBCONTROLS.DATAGRIDPAGECHANGEDEVENTARGS) HANDLES DGORDER.PAGEIDEXCHANGED

'Get the page number of the page

Dgorder.currentPageIndex = E.NewpageIndex

DGORDER.DataSource = GetDV (ViewState ("strsort"). TOSTRING ())

Dgorder.DATABIND ()

End Sub

The results shown are shown below: (Click the column header to be sorted)

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 = "