Asp.net|datagrid|datagrid Control | Sort the previous section we already know that the DataGrid sorting function is controlled by the AllowSorting attribute, and in this section we will validate this feature through an instance.
In the Datacon Web project, add a form named Datagrid_sample4.aspx, add a DataGrid control, and Datagrid_sample4.aspx's main HTML code is as follows:
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" ></FONT>
<asp:datagrid id= "DataGrid1"
runat= "Server" PageSize =6
Allowpaging= "true" allowsorting= "true" bordercolor= "#009900" borderwidth= "1px" cellpadding= "0" >
<alternatingitemstyle
Font-size= "X-small" backcolor= "WhiteSmoke" > </AlternatingItemStyle>
<itemstyle font-size= "X-small" backcolor= "#FFFFFF" > </ItemStyle>
<footerstyle wrap= "False" backcolor= "Lightgray" ></FooterStyle>
<pagerstyle font-size= "X-small" font-bold= "True" position= "Topandbottom" backcolor= "linen" Mode= "NumericPages" ></PagerStyle>
</asp:DataGrid>
</form>
</body>
The code in DataGrid_Sample4.aspx.vb is as follows:
'----Code begin----
' Introduce namespaces
Imports System
Imports System.Data
Imports System.Web.UI
Public Class datagrid_sample4_aspx
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' The code generated by the form designer is omitted here to save space
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' Use ViewState (' sort ') to save the DataGrid arrangement data by initializing the ID field
ViewState ("sort") = "id"
' Call the data-binding process
GetData ()
End Sub
' Read the data information process
Sub GetData ()
Dim Mycon as Oledb.oledbconnection
Try
Mycon = New oledb.oledbconnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data source=" + Server.MapPath (".") + "\ Studentinfor.mdb ")
Dim mycmd as Oledb.oledbdataadapter = New oledb.oledbdataadapter ("SELECT * from Student", mycon)
Dim dt as Data.dataset = New Data.dataset
myCMD. Fill (DT)
Dim dv as Data.dataview = New Data.dataview
' Declare the DataView class and instantiate it
DV = dt. Tables (0). DefaultView
Dv. Sort = ViewState ("Sort")
' Specifies the sort value of the DataView class, which is passed in by ViewState (' sort ')
DataGrid1.DataSource = DV
' Specifies that the DATAGRID1 data source is DataView
Datagrid1.databind ()
Catch ex as Exception
Response.Write ("program error, information described below:<br>" & Ex.) message.tostring)
Finally
Mycon. Close ()
End Try
End Sub
' Paging Event process
Private Sub datagrid1_pageindexchanged (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles datagrid1.pageindexchanged
Datagrid1.currentpageindex = E.newpageindex
GetData ()
End Sub
' Request an arrangement sequence event procedure
Private Sub Datagrid1_sortcommand (ByVal source as Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles Datagrid1.sortcommand
ViewState ("sort") = E.sortexpression.tostring
GetData ()
End Sub
End Class
'-----Code End----------
After you save the compilation, the effect is shown in Figure 9.9.
Figure 9.9 Datagrid_sample4.aspx Run Results