因此,這裏是場景 - 我通過查詢表transactions
和v_patients
的數據開始。我執行該查詢到List(Of transaction)
。我操縱(Concat()
,GroupBy()
等)列表,直到我到達GroupMatchListView的最終投影和DataBind()
。我在標記中有一些LinkButton,它們有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