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. Issue I'm having now is that even though the searches are clean and things are able to function I am...

问题:

Basically I recently added a search-box and button to my asp.net site. 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. Doesn't help that its in vb either. 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

回答1:

This could help you out.

The trick is to never rely on the order of the records within a DataTable. Use a DataView against the DataTable and then bind to the 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


回答2:

enter image description here

When you click Vendor_Name is should sort to the first or last in the column. 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.

  • 发表于 2019-03-26 20:43
  • 阅读 ( 176 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除