2012-04-20 37 views
0

因此,这里是场景 - 我通过查询表transactionsv_patients的数据开始。我执行该查询到List(Of transaction)。我操纵(Concat(),GroupBy()等)列表,直到我到达GroupMatchListView的最终投影和DataBind()。我在标记中有一些LinkBut​​ton,它们有Sort命令。现在我需要在排序列表的GroupMatchListView_Sorting事件处理程序中编写一些内容。我想我需要使用IComparer,或者GroupMatchListView.Items.OrderBy以某种方式对ListView进行排序,但我很困惑。我不想运行另一个SQL查询并重新执行我的列表操作。谢谢你的帮助!对自定义投影DataBound的ASP.NET ListView进行排序

最终投射:

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(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 

回答

0

好了,我不能GroupMatchesListView.Items做到这一点。相反,我将先前查询的结果投入到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 
    
  • 然后我的工作我的魔法

    保护小组GroupMatchListView_Sorting(发送者为对象,E为系统。 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 
    

希望这可以帮助别人。

相关问题