0

所以这是场景 - 我首先从表transactionsv_patients. 我将该查询执行到List(Of transaction). 我操纵(Concat()GroupBy()等)该列表,直到达到最终投影并DataBind()到达 GroupMatchListView。我在标记中有一些带有Sort命令的链接按钮。现在我需要在GroupMatchListView_Sorting对列表进行排序的事件处理程序中编写一些内容。我想我需要使用 IComparer,或者GroupMatchListView.Items.OrderBy以某种方式对 ListView 进行排序,但我很困惑。我不想运行另一个 SQL 查询并重做我的列表操作。谢谢你的帮助!

最终投影:

Dim goliath = From f In wholeResults _
                          Group f By f.v_patient Into Group _
                          Order By v_patient.last_name, v_patient.first_name, v_patient.date_of_birth _
                          Select New With {.PatientID = v_patient.patient_id, .LastName = v_patient.last_name, .FirstName = v_patient.first_name, _
                             .DOB = v_patient.date_of_birth, .Ins = v_patient.insname, .MatchCount = Group.Count(), .Matches = Group}

            GroupMatchListView.DataSource = goliath
            GroupMatchListView.DataBind()

我有一些具有排序命令的按钮:

<th id="Th4" runat="server" style="width: 100px" width="100px">
                                        <asp:LinkButton ID="SortLastNameButton" runat="server" CommandName="Sort" CommandArgument="LastName" ForeColor="White">Last Name</asp:LinkButton>
                                    </th>
                                    <th id="Th5" runat="server" style="width: 100px" width="100px">
                                        <asp:LinkButton ID="SortFirstNameButton" runat="server" CommandName="Sort" CommandArgument="FirstName" ForeColor="White">First Name</asp:LinkButton>
                                    </th>
                                    <th id="Th6" runat="server" style="width: 85px" align="center" width="85px">
                                        <asp:LinkButton ID="SortDOBButton" runat="server" CommandName="Sort" CommandArgument="DOB" ForeColor="White" ToolTip="Date of Birth">DOB</asp:LinkButton>
                                    </th>
                                    <th id="Th7" runat="server" style="width: 185px" width="185px">
                                        <asp:LinkButton ID="SortInsuranceButton" runat="server" CommandName="Sort" CommandArgument="Ins" ForeColor="White">Insurance</asp:LinkButton>
                                    </th>

我放置排序逻辑的地方:

 Protected Sub GroupMatchListView_Sorting(sender As Object, e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles GroupMatchListView.Sorting
        ''
        ''Something goes here that sorts the ListView
        ''Maybe something that uses
        ''IComparer
        ''or
        ''GroupMatchListView.Items.OrderBy()
    End Sub

再次感谢。

编辑:我认为我在正确的轨道上。我采用 GroupMatchListView.Items,使用 OrderBy,并将结果分配给 List(Of ListViewDatItems)。然后我清除 GroupMatchListView.Items,并从我的 ListViewDataItems 列表中添加回项目。但是,当我返回该页面时,什么都没有改变。如果我只使用 DataBind() 我会得到一个空的 ListView。如果我将 ListViewDataItems 列表分配为 DataSource,然后是 DataBind,则会出现错误。有人知道我怎么能把它包起来吗?

这是我的工作:

    Protected Sub GroupMatchListView_Sorting(sender As Object, e As System.Web.UI.WebControls.ListViewSortEventArgs) Handles GroupMatchListView.Sorting

            Dim caesar As List(Of ListViewDataItem) = (GroupMatchListView.Items.OrderBy(Function(i) CType(i.FindControl("FirstName"), Label).Text)).ToList()

            GroupMatchListView.Items.Clear()

            For Each i As ListViewDataItem In caesar
                GroupMatchListView.Items.Add(i)
            Next

            ''I don't know what comes next.  The following does not work:
            ''GroupMatchListView.DataSource = GroupMatchListView.Items
            ''GroupMatchListView.DataBind()
        End Sub
4

1 回答 1

0

好吧,我不能用 GroupMatchesListView.Items 做到这一点。相反,我将之前查询的结果放入 Session 变量中。然后到了排序的时候,我把它拿出来,排序,然后发回给 Session。

这是代码:

  • 首先我把它扔进 Session

        goliath = (From f In wholeResults _
                      Group f By f.v_patient Into Group _
                      Order By v_patient.last_name, v_patient.first_name, v_patient.date_of_birth _
                      Select New With {.PatientID = v_patient.patient_id, .LastName = v_patient.last_name, .FirstName = v_patient.first_name, _
                         .DOB = v_patient.date_of_birth, .Ins = v_patient.insname, .MatchCount = Group.Count(), .Matches = Group}).ToList()
    
    Session("ListOfMatches") = goliath
    GroupMatchListView.DataSource = goliath
    
  • 然后我施展了我的魔法

    Protected Sub GroupMatchListView_Sorting(sender As Object, e As System.Web.UI.WebControls.ListViewSortEventArgs) 处理 GroupMatchListView.Sorting

            Dim interimMatches As IEnumerable(Of Object) = CType(Session("ListOfMatches"), IEnumerable(Of Object))
    
            Select Case e.SortExpression
                Case "LastName"
                    If CurrentSortExpression = "LastName" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.LastName, String)).ToList()
                        CurrentSortExpression = "LastName"
                    End If
                Case "FirstName"
                    If CurrentSortExpression = "FirstName" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.FirstName, String)).ToList()
                        CurrentSortExpression = "FirstName"
                    End If
                Case "DOB"
                    If CurrentSortExpression = "DOB" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.DOB, Date)).ToList()
                        CurrentSortExpression = "DOB"
                    End If
                Case "Ins"
                    If CurrentSortExpression = "Ins" Then
                        interimMatches = interimMatches.Reverse()
                    Else
                        interimMatches = interimMatches.OrderBy(Function(i) CType(i.Ins, String)).ToList()
                        CurrentSortExpression = "Ins"
                    End If
            End Select
            Session("ListOfMatches") = interimMatches
            ViewState("CurrentSortExpression") = CurrentSortExpression
            GroupMatchListView.DataSource = interimMatches
            GroupMatchListView.DataBind()
    
        End Sub
    

希望这可以帮助某人。

于 2012-04-23T05:06:39.257 回答