The Code is as follows:
Private Sub Page_Load () Sub Page_Load (ByVal sender As System. Object, ByVal e As System. EventArgs) Handles MyBase. Load
If Not IsPostBack Then
If dgdBook. Attributes ("SortExpression") Is Nothing Then
DgdBook. Attributes ("SortExpression") = "dlh" 'adds a sorting attribute to the datagrid, and the default sorting expression is dlh.
DgdBook. Attributes ("SortDirection") = "ASC" 'adds a sort direction attribute to the datagrid, Which is sorted in ascending order by default.
End If
BindDataGrid ()
End If
End Sub
Private Sub BindDataGrid ()
'..
Dim dt As New DataTable
DataAdapter. Fill (dt)
Dim dv As DataView = dt. DefaultView
Dim SortExpression As String = dgdBook. Attributes ("SortExpression") 'sort expression
Dim SortDirection As String = dgdBook. Attributes ("SortDirection") 'sort direction
Dv. Sort = SortExpression + "" + SortDirection 'specifies the view sorting method;
DgdBook. AllowSorting = True
DgdBook. DataSource = dv
DgdBook. DataBind ()
End Sub
Private Sub dgdBook_SortCommand () Sub dgdBook_SortCommand (ByVal source As Object, ByVal e As System. Web. UI. WebControls. DataGridSortCommandEventArgs) Handles dgdBook. SortCommand
Dim SortExpression As String = dgdBook. Attributes ("SortExpression"). ToString 'get the original sorting Field
Dim SortDirection As String = dgdBook. Attributes ("SortDirection"). ToString 'get the original sorting direction
If e. SortExpression. ToString = SortExpression Then
SortDirection = IIf (SortDirection = "ASC", "DESC", "ASC ")'
Else' click different column headers to change the sorting fields and sort them in ascending order
SortExpression = e. SortExpression. ToString
SortDirection = "ASC"
End If
DgdBook. Attributes ("SortExpression") = SortExpression assign a new sorting Field
DgdBook. Attributes ("SortDirection") = SortDirection 'assign a new sorting direction
BindDataGrid ()
End Sub