使用 VB 在 ASP.Net 的 GridView 中排序事件

Sorting event in GridView in ASP.Net using VB

我对 Gridview 中的排序有疑问。我不是 vb.net 方面的专家,但我必须解决这个问题。我想解释一下我的数据是如何进入 GridView 的。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
//also there is some logic in there but I think that part of the code will not effect
  loadgrid()
End Sub

loadgrid() 将通过一些步骤加载查询数据

//这个函数用于一些逻辑

Private Sub loadgrid() Handles B_SEARCH.Click, chkLegacy.CheckedChanged, gvEmployer.PageIndexChanged
        GridDataLoader() 
    End Sub

eEmployer 将获取 gridView

查询的所有数据
Public Sub GridDataLoader()
       //some code was there because of searching
        Dim dataTable = Employer.getEmployers(eEmployer, chkLegacy.Checked)
        gvEmployer.DataBind()
    End Sub
Public Function GetEmployers(ByVal eEmployer As tblEmployer, ByVal All As Boolean, Optional ByVal sortExpression As String = Nothing) As DataTable
        Dim query = ""
        query =
                "select employer.EmployerID as EmployerId,
                employer.Employer_Name as EmployerName,
           // the query is so large so i delete all for better understanding
                on (employer.Modified_by=tum.UserID)
                where employer.LegacyID IS NULL  and address.ValidityTo is null"
        'End If

        Dim params = ""
        If All = False Then
            query += " AND employer.ValidityTo is null"
        End If

        If (params.Trim() IsNot "") Then
            query = query & params
        End If
        data.setSQLCommand(query, CommandType.Text)
        Return data.Filldata
    End Function

最后,数据返回到网格视图中。但我的问题是我不明白如何实现排序。我改变了一些东西是视图 AllowSorting="true" SortExpression="EmployerName" 我不知道我应该进一步做什么。我正在关注这个 Articel

嗯,基本的设置可以是这样的:

并且始终在您的所有网页中使用 Not IsPost 后存根。

所以,我有这个标记:

(我用的是向导-创建数据源)。然后我吹掉数据源设置,并从网页中删除 DataSourc1。

所以,我有这个标记:

   <div style="width:40%;padding:25px">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table table-hover" AllowSorting="True" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" SortExpression="HotelName" />
                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            </Columns>
        </asp:GridView>
        <br />
    </div>

我要加载的代码是这样的: (请注意我的 LoadGrid 视图如何具有“默认”排序

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        LoadGrid
    End If
End Sub

Sub LoadGrid(Optional strSort = "HotelName")

    GridView1.DataSource = MyRst("SELECT * FROM tblHotels Order by " & strSort)
    GridView1.DataBind()

End Sub

我的排序事件存根是这样的:

Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

    LoadGrid(e.SortExpression)

End Sub

结果是这样的:

现在当然我总是厌倦了必须键入连接和代码来创建记录集 (DataTable),所以我有这个全局帮助程序:

Public Function MyRst(strSQL As String) As DataTable

    Dim rstData As New DataTable
    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            rstData.Load(cmdSQL.ExecuteReader)
        End Using
    End Using

    Return rstData
End Function

编辑:

所以另一个问题是如何对 ASC 和 DESC 进行排序。

好吧,如果您再次单击标题,我们可以添加,我们会反转排序。

这需要更多的代码,但这是可行的:

Sub LoadGrid(Optional strSort As String = "HotelName",
             Optional SortASC As Boolean = True)

    Dim rstTable As DataTable
    rstTable = MyRst("SELECT * FROM tblHotels")
    rstTable.DefaultView.Sort = strSort & " " & If(SortASC, "ASC", "DESC")
    GridView1.DataSource = rstTable
    GridView1.DataBind()

    ViewState("Sort") = strSort
    ViewState("SortASC") = SortASC
End Sub

Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting

    If ViewState("Sort") = e.SortExpression Then
        ViewState("SortASC") = Not ViewState("SortASC")
    Else
        ViewState("SortASC") = True
    End If
    LoadGrid(e.SortExpression, ViewState("SortASC"))

End Sub