搜索后排序-Gridview1触发了事件未处理的事件

时间:2019-03-21 19:24:29

标签: c# sql asp.net vb.net

基本上,我最近在asp.net网站上添加了一个搜索框和一个按钮。我现在遇到的问题是,即使搜索是干净的并且可以正常工作,但在运行任何搜索之前和之后都存在排序方面的问题。我觉得sda(Fill).dt之后可能会丢失一些东西,但是我在网上发现的任何内容似乎都无济于事。在vb中也无济于事。预先感谢。

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports System.Collections
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports Microsoft.VisualBasic
Public Class ShowPOsAdmin
    Inherits System.Web.UI.Page
    Dim strConn As String = ConfigurationManager.ConnectionStrings("PurchaseOrderConnectionString").ConnectionString
    Dim Connection As SqlConnection = New SqlConnection(strConn)
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            Me.SearchVen()
        End If
    End Sub
    Private Sub SearchVen()
        Dim Constr As String = ConfigurationManager.ConnectionStrings("PurchaseOrderConnectionString").ConnectionString
        Using Con As New SqlConnection(Constr)
            Using cmd As New SqlCommand
                Dim searchword As String = "SELECT PurchaseOrder.PoId, PurchaseOrder.Vendor_Name, PurchaseOrder.POAmount,PurchaseOrder.DateFrom, PurchaseOrder.DateTo, PurchaseOrder.Balance, PurchaseOrder.CodeId, PurchaseOrder.PoNumber, BPNumber, ClassCode.CodeId AS Expr1, ClassCode.CodeDefinition, PurchaseOrder.Notes FROM PurchaseOrder INNER JOIN ClassCode ON PurchaseOrder.CodeId = ClassCode.CodeId"
                If Not String.IsNullOrEmpty(TextBox11.Text.Trim()) Then
                    searchword += " Where PurchaseOrder.PONumber Like @POnumber + '%'"
                    cmd.Parameters.AddWithValue("PONumber", TextBox11.Text.Trim())
                End If
                cmd.CommandText = searchword
                cmd.Connection = Connection
                Using sda As New SqlDataAdapter(cmd)
                    Dim dt As New DataTable()
                    sda.Fill(dt)
                    dt.DefaultView.Sort = "Vendor_Name ASC"
                    GridView1.DataSourceID = ""
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                    ViewState("dt") = dt
                End Using
            End Using
        End Using
    End Sub
    Private Sub DetailsView1_ItemDeleted(sender As Object, e As DetailsViewDeletedEventArgs) Handles DetailsView1.ItemDeleted
        GridView1.DataBind()
        Me.SearchVen()
    End Sub

    Private Sub DetailsView1_ItemInserted(sender As Object, e As DetailsViewInsertedEventArgs) Handles DetailsView1.ItemInserted
        GridView1.DataBind()
        Me.SearchVen()
    End Sub
    Private Sub DetailsView1_ItemUpdated(sender As Object, e As DetailsViewUpdatedEventArgs) Handles DetailsView1.ItemUpdated
        GridView1.DataBind()
        Me.SearchVen()
    End Sub
    Private Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        Me.SearchVen()
    End Sub
    Protected Sub TextBox11_TextChanged(sender As Object, e As EventArgs) Handles TextBox11.TextChanged
        Me.SearchVen()
    End Sub
    Protected Sub DetailsView1_PageIndexChanging(sender As Object, e As DetailsViewPageEventArgs) Handles DetailsView1.PageIndexChanging
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

这可以帮助您。

  

诀窍是永远不要依赖记录中的记录顺序   数据表。对DataTable使用DataView,然后绑定到   数据视图。

Imports System.Data.SqlClient
Imports System.Data

Partial Class PageInitTest
    Inherits System.Web.UI.Page

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

        If Not IsPostBack Then

            Dim conn As New SqlConnection("myconnectionstring")
            Dim command As New SqlCommand("mySQL", conn)
            Dim da As New SqlDataAdapter(command)
            Dim tblData As New DataTable
            da.Fill(tblData)

            ' now the sorting
            Dim dv As New DataView(tblData)

            ' you can also do filtering to not show all of them
            'dv.RowFilter = "MyField = 1"

            dv.Sort = "MyField ASC"    ' ASC or DESC

            ' then bind the control to the DataView, not the DataTable
            dgdMyData.DataSource = dv
            dgdMyData.DataBind()

            conn.Close()

        End If

    End Sub

End Class

链接中的示例:

https://forums.asp.net/t/1063235.aspx?Table+Adapter+problem+with+sort+order

答案 1 :(得分:-1)

enter image description here

当您单击Vendor_Name时,应排在该列的第一位或最后一位。问题是由于搜索,实际上并没有这样做。当您单击标题中的一个标题(如“供应商名称”或“日期”)时,就会收到该错误消息。