【问题标题】:Cannot find column when Sorting Gridview对 Gridview 进行排序时找不到列
【发布时间】:2012-07-17 15:25:45
【问题描述】:

我需要帮助对 gridview 进行排序,下面是我的代码:

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

    'Retrieve the table from the session object.
    Dim dt As DataView = dsData.AsDataView


    If dt IsNot Nothing Then

        'Sort the data.
        If (GridView1.SortDirection() = SortDirection.Ascending) Then
            dt.Sort = e.SortExpression & " " & SortDirection.Descending
        Else
            dt.Sort = e.SortExpression & " " & SortDirection.Descending
        End If

        GridView1.DataSource = dt
        GridView1.DataBind()

    End If

我目前得到: 找不到列 [我正在排序的任何列]

我是 VB 新手,正在努力学习这一点,我无法使任何其他示例代码正常工作。

感谢您的帮助。

添加: 其他代码:

在页面加载时我运行我的存储过程,然后它绑定数据:

    Dim objConn As IDbConnection = Nothing
    Dim strBlder As New System.Text.StringBuilder
    Dim providerID As String = Request.QueryString("ProviderID")
    Dim providerName As String = Request.QueryString("ProviderName")
    GridView1.PagerSettings.Mode = PagerButtons.NumericFirstLast

存储过程查询:

    GridView1.DataSource = dsData
    GridView1.AllowSorting = True
    GridView1.DataBind()

ASPX 代码:

    <asp:GridView ID="GridView1" runat="server" style="margin-right: 0px" 
        AllowPaging = "True" PageSize = "50" AllowSorting = "true">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:TemplateField HeaderText="Status"></asp:TemplateField>
        </Columns>
    </asp:GridView>

【问题讨论】:

  • 你调试过代码吗?设置断点并检查快速观察窗口中的值。
  • 我有并且 DT 中的表有一列与我要排序的列相匹配。它总是在这一行崩溃:dt.Sort = e.SortExpression & " " & SortDirection.Descending

标签: vb.net visual-studio sorting gridview


【解决方案1】:

替换

If (GridView1.SortDirection() = SortDirection.Ascending) Then
    dt.Sort = e.SortExpression & " " & SortDirection.Descending
Else
    dt.Sort = e.SortExpression & " " & SortDirection.Descending
End If

If (GridView1.SortDirection() = SortDirection.Ascending) Then
    dt.Sort = e.SortExpression & " ASC"
Else
    dt.Sort = e.SortExpression & " DESC"
End If

【讨论】:

  • 谢谢!但是为什么我不能将 sortDirection 与 Ascending 进行比较?我需要将它与列或其他东西进行比较吗? If (GridView1.SortDirection() = SortDirection.Ascending)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多