Let the ASP.NET's DataGrid can be sorted, selectable, available

xiaoxiao2021-03-05  29

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:

l Add a DataGrid and named DGORDER.

l Add a template column, put a CHECKBOX control called CB in the template column. This column is used to achieve the selection

l Plus the sorting expression sortexpression for each column to be sorted.

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

l Set the 15 row of data per page per page, allowPaging = "true" to allow paging.

Whole HTML page code:

Backstage code:

Get the data view, the parameters are columns 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 (byval sender 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 Source As Object,

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 SOURCE As Object,

Byval e as system.Web.ui.WebControls.DataGridPageChangeDeventargs) Handles Dgorder.pageIndexchanged

'Get the page number of the page

Dgorder.currentPageIndex = E.NewpageIndex

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

Dgorder.DATABIND ()

End Sub

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