无法在使用VB.NET中的DataSet的GridView中进行排序

时间:2016-07-15 16:29:20

标签: asp.net vb.net sorting visual-studio-2012 gridview

我尝试对GridView进行排序,该DataSet有6列由stored procedure数据绑定。所有这些数据都是通过 <asp:GridView ID="dgvBranchChange" runat="server" AutoGenerateColumns="False" AllowPaging="True" CssClass="ReportDataGrid" HeaderStyle-CssClass="DataGridHeader" RowStyle-CssClass="AccentShade" AlternatingRowStyle-CssClass="NoShade" SelectedRowStyle-CssClass="AccentLvl3" PagerSettings-Mode="NumericFirstLast" PagerStyle-HorizontalAlign="center" PagerStyle-CssClass="paging" Width="100%" PageSize="25" AllowSorting="True" ShowHeaderWhenEmpty="true" OnSorting="dgvBranchChange_Sorting"> <AlternatingRowStyle CssClass="NoShade"></AlternatingRowStyle> <Columns> <asp:TemplateField HeaderText="LoanOfficerID" Visible="false" SortExpression="LoanOfficerID"> <ItemTemplate> <asp:Label ID="lblBULOID" runat="server" Text='<%# Eval("LO_ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LoanOfficer" HeaderStyle-HorizontalAlign="Center" SortExpression="LoanOfficer"> <ItemTemplate> <asp:Label ID="lblBULOName" runat="server" Text='<%# Eval("LO_Name")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Branch_NameID" Visible="false" SortExpression="Branch_NameID"> <ItemTemplate> <asp:Label ID="lblBUBranchID" runat="server" Text='<%# Eval("Branch_ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Branch" HeaderStyle-HorizontalAlign="Center" SortExpression="Branch"> <ItemTemplate> <asp:Label ID="lblBUBranchName" runat="server" Text='<%# Eval("Branch_Name")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Move_Begin_Date" HeaderStyle-HorizontalAlign="Center" SortExpression="Move_Begin_Date"> <ItemTemplate> <asp:Label ID="lblBUBeginDate" runat="server" Text='<%# string.format("{0:MMM yyyy}",Eval("Begin_Date"))%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Move_End_Date" HeaderStyle-HorizontalAlign="Center" SortExpression="Move_End_Date"> <ItemTemplate> <asp:Label ID="lblBUEndDate" runat="server" Text='<%# string.format("{0:MMM yyyy}",Eval("End_Date"))%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="User_who_Added_Change" HeaderStyle-HorizontalAlign="Center" SortExpression="User_who_Added_Change"> <ItemTemplate> <asp:Label ID="lblBUUserId" runat="server" Text='<%# Eval("User_ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Change_Date" HeaderStyle-HorizontalAlign="Center" SortExpression="Change_Date"> <ItemTemplate> <asp:Label ID="lblBUCreationDate" runat="server" Text='<%# string.format("{0:MM/dd/yyyy}",Eval("Create_Date"))%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ButtonType="Link" HeaderText="Remove Change" ShowDeleteButton="true" DeleteText="Remove" /> </Columns> </asp:GridView> 调用的。我已经浏览了interweb上的不同帖子和教程,最后得到了我认为最适合我的页面的帖子。但是有了这个,我不断收到错误,

  

异常类型:System.IndexOutOfRangeException
  消息:找不到列LoanOfficer

我打开这个方法的帮助,甚至建议采用不同的方法来解决排序问题。 这是我的代码:
ASPX

    Protected Sub dgvBranchChange_Sorting(sender As Object, e As GridViewSortEventArgs)

    'Retrieve the current table
    Dim dsBranchChange As DataSet
    Dim asParams(0) As IDbDataParameter
    Dim lDB As New RHubCoreFunctions.RHubDB
    asParams(0) = lDB.dpCreateDataParameter("@AccountID", DbType.String, 8, AccountSelected.AccountID)
    dsBranchChange = lDB.GetDataSet("ssp_Account_LO_Branch_Change", asParams)

    Dim dt As DataView = dsBranchChange.Tables(0).AsDataView

    If dt IsNot Nothing Then

        'sort the data
        If (dgvBranchChange.SortDirection() = SortDirection.Ascending) Then
            dt.Sort = e.SortExpression & " ASC"
        Else
            dt.Sort = e.SortExpression & " DESC"
        End If

        dgvBranchChange.DataSource = dt
        dgvBranchChange.DataBind()
    End If

End Sub

背后的代码

class MainDialog(QDialog, Ui_mainDialog):
    def __init__(self, parent=None):
        ..
        # use new-style connections
        self.threadclass.gfxProgress.connect(self.setProgress)
        self.threadclass.textProgress.connect(self.setTextLabel)
        self.pushButton.clicked.connect(self.threadclass.doSomething)   


class ThreadClass(QtCore.QThread):
    # define new-style signals
    gfxProgress = QtCore.Signal(int)
    textProgress = QtCore.Signal(str)

    def __init__(self, parent=None):
        super(ThreadClass, self).__init__(parent)

    def doSomething(self):
        # start the thread
        # by default, this will automatically call run()
        self.start()

    def run(self):
        for i in range(1, 100):
            print("Status at : %s " % i)
            # emit the new-style signals
            self.gfxProgress.emit(i)
            self.textProgress.emit(str(i))
            time.sleep(1)

修改 我发现我使用了错误的SortExpression。我使用与标题文本相同的那个,就像在几乎所有的演练/教程中给出的那样。但它必须与您从中获取数据的表中的列名相同,即SQL表。

但现在我只能对升序中的第一列进行排序,仅此而已

1 个答案:

答案 0 :(得分:0)

因为您必须在Session中保存最后一个排序表达式。

请查看下面的链接以获取更多信息,代码在C#中,但您可以轻松转换为VB。

GridView sorting: SortDirection always Ascending

我希望能帮助你。