单击事件时Gridview无法运行

时间:2014-05-06 03:01:25

标签: asp.net vb.net gridview

我的网格视图无效。当我点击标题上的排序时,它就像无法正常工作。注意到错误并注意到变化。希望我能解释我的问题。没有责备区。只想在这里得到解决方案。 在我的代码下面,我一直在尝试但注意到我的系统发生了。 grid view

vb.net

Sorting

希望我能得到解释。

2 个答案:

答案 0 :(得分:0)

我想问题是由于在代码中应用sort时连接两个字符串:

dt.DefaultView.Sort=SortExpression & direction

请尝试以下示例:

<asp:TemplateField HeaderText="As Of Sales" SortExpression= "asofsales">

这将解决您的问题以允许排序,

Private Sub SortGridView(ByVal sortExpression As String, ByVal direction As String)

            If direction = "1" Then
                direction = "ASC"
            Else
                direction = "DESC"
            End If

            dt.DefaultView.Sort=SortExpression + " " + direction

            GridView1.DataSource = dt

            GridView1.DataBind()

        End Sub

    End Class

在你使用之前             dt.DefaultView.Sort=SortExpression & direction,这将无法工作,因为它会连接两个字符串并将它们用作列名,这将导致程序找不到列。所以只需将其改为

    dt.DefaultView.Sort=SortExpression  + " " + direction

答案 1 :(得分:-1)

我无法在您的图片中正确查看代码。

请参阅:

<强>更新

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub TaskGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)

    'Retrieve the table from the session object.
    Dim dt = TryCast(Session("TaskTable"), DataTable)

    If dt IsNot Nothing Then

      'Sort the data.
      dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
      TaskGridView.DataSource = Session("TaskTable")
      TaskGridView.DataBind()

    End If

  End Sub


  Private Function GetSortDirection(ByVal column As String) As String

    ' By default, set the sort direction to ascending.
    Dim sortDirection = "ASC"

    ' Retrieve the last column that was sorted.
    Dim sortExpression = TryCast(ViewState("SortExpression"), String)

    If sortExpression IsNot Nothing Then
      ' Check if the same column is being sorted.
      ' Otherwise, the default value can be returned.
      If sortExpression = column Then
        Dim lastDirection = TryCast(ViewState("SortDirection"), String)
        If lastDirection IsNot Nothing _
          AndAlso lastDirection = "ASC" Then

          sortDirection = "DESC"

        End If
      End If
    End If

    ' Save new values in ViewState.
    ViewState("SortDirection") = sortDirection
    ViewState("SortExpression") = column

    Return sortDirection

  End Function

  Protected Sub Page_Load()

    If Not Page.IsPostBack Then

      ' Create a new table.
      Dim taskTable As New DataTable("TaskList")

      ' Create the columns.
      taskTable.Columns.Add("Id", GetType(Integer))
      taskTable.Columns.Add("Description", GetType(String))

      'Add data to the new table.
      For i = 0 To 9
        Dim tableRow As DataRow = taskTable.NewRow()
        tableRow("Id") = i
        tableRow("Description") = "Task " & (10 - i)
        taskTable.Rows.Add(tableRow)
      Next i

      'Persist the table in the Session object.
      Session("TaskTable") = taskTable

      'Bind the GridView control to the data source.
      TaskGridView.DataSource = Session("TaskTable")
      TaskGridView.DataBind()
    End If

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Sorting example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

      <asp:GridView ID="TaskGridView" runat="server" 
        AllowSorting="true" 
        OnSorting="TaskGridView_Sorting" >
      </asp:GridView>

    </div>
    </form>
</body>
</html>