1

我正在使用 Gridview 显示一个表中的数据,但是使用下面的代码它不允许我自动排序,我收到一条错误消息,指出“System.Web.HttpException:GridView 'GridView1' 触发事件排序未处理"

它应该自动排序,而不需要我为排序事件编写代码,我已经使它工作但通过 ASPX 文件,但我不想使用它,因为我想更改查询以执行不同的过滤器所以我想要从 .vb 文件而不是 .aspx 文件执行此操作

这是我的代码

Imports System.Data.SqlClient



Public Class WebForm1
   Inherits System.Web.UI.Page

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

      Dim cnnData As New SqlConnection(ConfigurationManager.ConnectionStrings("DataTestConnectionString").ConnectionString)
    'Dim adData As New SqlDataAdapter
    Dim dsData As New DataSet
    Dim strQry As String

    strQry = "SELECT Student_No, " & _
                    "FName, " & _
                    "MName, " & _
                    "LName, " & _
                    "Phone_No, " & _
                    "Major, " & _
                    "Start_Date, " & _
                    "Status " & _
             "FROM tblStudent"



    Dim adData As New SqlDataAdapter(strQry, cnnData)

    adData.Fill(dsData)
    GridView1.DataSource = dsData

    GridView1.DataBind()



    'cnnData.Close()
    'adData.Dispose()

End Sub

End Class

这是我的 aspx 标记:

 <asp:GridView 
         ID="GridView1" 
         runat="server" 
         AllowPaging="True" 
         AllowSorting="True" 
         AutoGenerateColumns="False" 
         DataKeyNames="Student_No" 
         Height="30%" 
         Width="90%"
         CssClass="GridViewCSS"
         PagerStyle-CssClass="GridViewPager" 
         AlternatingRowStyle-CssClass="GridViewAlt"
         DataGridViewLinkColumn="Student_No"
         >


  <AlternatingRowStyle CssClass="GridViewAlt"></AlternatingRowStyle>


        <Columns>


        <asp:hyperlinkfield 
             DataTextField ="Student_No"
             navigateurl="./studentedit.aspx"            
             headertext="Employee No"
             />

            <asp:BoundField 
                 DataField="Student_No" 
                 HeaderText="Student No" 
                 ReadOnly="True" 
                 SortExpression="Student_No" 
                 FooterStyle-Font-Underline ="true"
                 />


            <asp:BoundField 
                 DataField="FName" 
                 HeaderText="First Name" 
                 ReadOnly="True" 
                 SortExpression="FName" 
                 />

            <asp:BoundField 
                 DataField="MName" 
                 HeaderText="Middle Initial" 
                 ReadOnly="True" 
                 SortExpression="MName" 
                 />

            <asp:BoundField 
                 DataField="LName" 
                 HeaderText="Last Name" 
                 ReadOnly="True" 
                 SortExpression="LName" 
                 />


            <asp:BoundField 
                 DataField="Phone_No" 
                 HeaderText="Phone Number" 
                 ReadOnly="True" 
                 SortExpression="Phone_No" 
                 />

            <asp:BoundField 
                 DataField="Major" 
                 HeaderText="Major" 
                 ReadOnly="True" 
                 SortExpression="Major" 
                 />

            <asp:BoundField 
                 DataField="start_date" 
                 HeaderText="Start Date" 
                 ReadOnly="True" 
                 SortExpression="Start_Date" 
                 />

            <asp:BoundField 
                 DataField="Status" 
                 HeaderText="Status" 
                 ReadOnly="True" 
                 SortExpression="Status" 
                 />

        </Columns>

 <PagerStyle CssClass="GridViewPager"></PagerStyle>
      </asp:GridView

帮助表示赞赏。

谢谢!

4

2 回答 2

0

I don't know about your ASP, but I have a little trick for sorting the DataTable itself in VB. As you know, you can't directly sort a datatable. You can, however, sort a dataview. The trick is to create a view of your table, sort the view, then convert your view into a new table.

With the below function you can pass in your datatable, the column name to sort on, and ASC or DESC for ascending or descending. You can remove the "order" parameter from the function if you want and the view will always sort ascending. You can then bind the table to your GrindView. You can also send a string of multiple sortColumns if you want.

Create the function:

Public Function SortMyTable(ByVal ds As DataTable,_
     sortColumn As String, order As String) As DataTable

    ' Create a DataView of your original dataset 
    Dim view As New DataView(ds)
    ' Sort your data view.
    view.Sort = sortColumn + " " + order 
    ' Put your dataview into a new datatable
    Dim dt As DataTable = view.ToTable()

    ' Return your newly sorted datatable
    Return dt
End Function

Using the function:

Dim sortColumn As String = "MyColumnName"
Dim order As String = "ASC"

' Create your dataset
Dim ds As New DataTable
' fill your DataTable

' Use the function to return a new sorted dataset
Dim dt As DataTable = SortMyTable(ds, column, order)
'dt is a new datatable, sorted how you want it.

Good luck!

References: http://msdn.microsoft.com/en-us/library/system.data.dataview.sort.aspx

于 2013-10-22T23:23:10.677 回答
0

嘿,您还可以通过 SQL 查询对显示数据进行排序:

trQry = "SELECT Student_No, " & _
                "FName, " & _
                "MName, " & _
                "LName, " & _
                "Phone_No, " & _
                "Major, " & _
                "Start_Date, " & _
                "Status " & _
         "FROM tblStudent **order by FName**"

您可以以任何方式排序,在上面我根据 FName 明智地对 tblstudent 进行排序。

于 2014-01-08T10:13:25.867 回答