简体   繁体   English

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

[英]Sorting after search - gridview1 fired event Sorting which wasn't handled

Basically I recently added a search-box and button to my asp.net site. 基本上,我最近在asp.net网站上添加了一个搜索框和一个按钮。 Issue I'm having now is that even though the searches are clean and things are able to function I am having an issue with sorting both before running any search and afterwards. 我现在遇到的问题是,即使搜索是干净的并且可以正常工作,但在运行任何搜索之前和之后都存在排序方面的问题。 I feel like I might be missing something after sda(Fill).dt but nothing I've found online seems to help. 我觉得sda(Fill).dt之后可能会丢失一些东西,但是我在网上发现的任何内容似乎都无济于事。 Doesn't help that its in vb either. 在vb中也无济于事。 Thanks in advance. 提前致谢。

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

This could help you out. 这可以帮助您。

The trick is to never rely on the order of the records within a DataTable. 诀窍是永远不要依赖DataTable中记录的顺序。 Use a DataView against the DataTable and then bind to the DataView. 对DataTable使用DataView,然后绑定到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

Example in the link: 链接中的示例:

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

在此处输入图片说明

When you click Vendor_Name is should sort to the first or last in the column. 当您单击Vendor_Name时,应排在该列的第一位或最后一位。 The issue is because of the search it's not actually doing that. 问题是由于搜索,实际上并没有这样做。 The moment you click one of the headers like Vendor Name or Date from you get that error message. 从您单击标题之一(如供应商名称或日期)的那一刻起,您就会收到该错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM