Implement sort IRD in ascending and descending order

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.