(Domestic version)
I have been studying the basic functions of the ADO. Net object and the DataGrid Control over the past few days. Here are some tips to share with you.
Generally, the DataGrid Control in winform provides the column sorting function, but it does not exist in webform... In fact, we can use dataview In the ADO. Net object to achieve the same effect, but we only need to write a little moreCode. In addition, we also needAllowsortingChange the attribute value to true.
In code behind page, we only need to read the name of the selected column in The datagrid?sortcommand event, and then read the data from the data, just as if the data is hard to read, only data in dataset is transferred to a new dataview object, and dataview. change the sort attribute value to the column name obtained previously. The last step is to bind dataview to the DataGrid Control... This is all done!
Private Sortstring As String
Private Pageindex As Integer = - 1
Private Sub page_load () Sub Page_load ( Byval Sender As System. Object , Byval E As System. eventargs) Handles Mybase . Load
If Not Page. ispostback Then
Loaddatafromsql ()
End If
End sub
Private Sub loaddatafromsql () Sub Loaddatafromsql ()
' Declare Variables
Dim CN As Sqlconnection
Dim Da As Sqldataadapter
Dim DS As Dataset
Dim DV As System. Data. dataview
Try
' Connect to SQL database
CN = New Sqlconnection ( " Data Source = localhost; initial catalog = northwind; Integrated Security = true; " )
CN. open ()
' Use sqldataadapter to reprint the data returned by the database into the DataSet object.
DS = New Dataset
Da = New Sqldataadapter ( " Select customerid, companyName, contactname from MERs " , CN)
Da. Fill (DS, " MERs " )
' Create a new dataview object and use the column data selected by sort
DV = New System. Data. dataview (Ds. Tables ( " MERs " ))
DV. Sort = Sortstring
' Bind the obtained dataview object to the DataGrid Control.
Datagrid1.datasource = DV
If Pageindex <> - 1 Then Datagrid1.currentpageindex = Pageindex
Datagrid1.databind ()
Catch Ex As Sqlexception
' Show error message
Lblsqlerror. Text = " Error No .( " & Ex. Number & " ), Line No .( " & Ex. linenumber & " ), " & Ex. Message
Finally
' Release resources used
If Not CN Is Nothing Then
If CN. State <> Connectionstate. Closed Then CN. Close ()
CN = Nothing
End If
End Try
End sub
Private Sub datagrid1_sortcommand () Sub Datagrid1_sortcommand ( Byval Source As Object , Byval E As System. Web. UI. webcontrols. datagridsortcommandeventargs) Handles Datagrid1.sortcommand
' This is the click event process of the column header in the DataGrid.
' Read the selected column name
Sortstring = E. sortexpression. tostring ()
' Refresh the displayed information
Loaddatafromsql ()
End sub
Private Sub datagrid1_pageindexchanged () Sub Datagrid1_pageindexchanged ( Byval Source As Object , Byval E As System. Web. UI. webcontrols. datagridpagechangedeventargs) Handles Datagrid1.pageindexchanged
' This is the process of changing the page index in the DataGrid.
' Read the selected page index
Pageindex = E. newpageindex
' Refresh the displayed information
Loaddatafromsql ()
End sub
Click "Next" to download the program.