单击排序列后,Gridview无法重新绑定数据

时间:2014-07-21 16:20:43

标签: asp.net vb.net sorting gridview

我的应用程序由一个DropDownList控件组成,该控件显示一个gridview,其中包含与从DropDownList中选择的名称相关的数据。我正在尝试向网格添加排序功能,但是在从DropDownList中选择一个名称,然后在Gridview中显示相应的数据后,我点击一个列名进行排序,然后gridView变为空白(点击后)列名称。)

超文本:

<asp:GridView ID="gvShipments" runat="server" Width="718px" EmptyDataText="PO Number Not Found" AutoGenerateColumns ="false" CssClass="Table_default"  AllowSorting="True" OnSorting="gvShipments_Sorting">
                <Columns>
                    <asp:BoundField DataField="shipment_guid" HeaderText="Guid" Visible ="false"  />
                    <asp:BoundField DataField="company_name" HeaderText="Supplier" SortExpression="company_name" />
                    <asp:HyperLinkField DataTextField ="ponumber" DataNavigateUrlFields ="shipment_guid" DataNavigateUrlFormatString ="~/securepages/shipmentaddedit.aspx?shipment_guid={0}" headertext="PO" SortExpression="ponumber" />
                    <asp:BoundField DataField="Carrier_Name" HeaderText="Carrier" SortExpression="Carrier_Name" />
                    <asp:BoundField DataField="TrackingLink" HeaderText="Tracking#" SortExpression="TrackingLink"  />
                    <asp:BoundField DataField="Plant_Name" HeaderText="Shipped To" SortExpression="Plant_Name"  />
                </Columns>
        </asp:GridView> 

背后的代码:

页面加载:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then

            DropDownList1.Items.Insert(0, New ListItem("---Select---", "---Select---"))
            FillDropDownList()

            ViewState("SortExpression") = " Shipment_Guid ASC"
            'gvShipments.DataBind()
        End If
    End Sub

GridView排序事件:

' GridView Sorting Event 
    Protected Sub gvShipments_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
        Dim strSortExpression As String() = ViewState("SortExpression").ToString().Split(" "c)

        ' If the sorting column is the same as the previous one, 
        ' then change the sort order.
        If strSortExpression(0) = e.SortExpression Then
            If strSortExpression(1) = "ASC" Then
                ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "DESC"
            Else
                ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC"
            End If
        Else
            ' If sorting column is another column,  
            ' then specify the sort order to "Ascending".
            ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC"
        End If

        ' Rebind the GridView control to show sorted data.
        gvShipments.DataBind()
    End Sub

在Gridview中显示数据:

 Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        s = WebConfigurationManager.ConnectionStrings("ShipperNotificationConnectionString").ConnectionString
        con = New SqlConnection(s)
        con.Open()
        cmd = New SqlCommand("SELECT Shipment.Shipment_Guid, SupplierCompany.company_name, Shipment_Po.PONumber, Carrier.Carrier_Name, Shipment.TrackingLink, Plant.Plant_Name FROM Plant INNER JOIN Shipment ON Plant.Plant_ID = Shipment.Plant_id INNER JOIN SupplierCompany ON Shipment.Company_Guid = SupplierCompany.Company_guid INNER JOIN Shipment_Po ON Shipment.Shipment_Guid = Shipment_Po.Shipment_guid INNER JOIN Carrier ON Shipment.Carrier_Id = Carrier.Carrier_ID WHERE Plant.Plant_Name ='" + DropDownList1.SelectedItem.ToString() + "'" + "ORDER BY SupplierCompany.company_name ASC ", con)
        dr = cmd.ExecuteReader()
        gvShipments.DataSource = dr
        gvShipments.DataBind()
        dr.Close()
        con.Close()
    End Sub

请问我在这里做错了什么?提前致谢

1 个答案:

答案 0 :(得分:0)

我通过基本上使用sqlDataSources并将其绑定到Gridview

来解决了这个问题