我正在使用 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
帮助表示赞赏。
谢谢!