且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Datagrid中对来自数据库的项进行排序

更新时间:2023-10-08 09:29:58

好的,



你最初选择数据时应该这样做但是如果这不可行,你可以使用以下



解决方案1 ​​ - 更好的解决方案可以降低开销,让SQL提供商/服务器完成工作。

  Dim  strSQL  as   string  =   SELECT * FROM TravelData ORDER BY DepartureDate ASC,ArrivalDate ASC 





解决方案2:

 私人  Sub  GrabData()
' 您的数据集
Dim 已退回DataSet As DataSet

' ######
' 在此处执行数据集的填充
' ######

' 执行排序
Dim tmpDataView 作为 DataView(ReturnedDataSet.Tables( 0 ))使用 {.Sort = DepartureDate ASC,ArriveDate ASC}

' 删除原文并添加
ReturnedDataSet.Tables.Remove( 0 ):ReturnedDataSet.Tables.Add(tmpDataView.ToTable)

MyDataGrid.DataSource = ReturnedDataSet
MyDataGrid.DataBind()


结束 Sub


<asp:TemplateField HeaderText="Departure Date" meta:resourcekey="TemplateFieldResource7"   >
                       <ItemTemplate>
                           <asp:Repeater ID="repSector" runat="server" DataSource='<%# CType(Container.DataItem,System.Data.DataRowView).Row.GetChildRows("Booking_Part_TO_Sector_FK") %>'>
                               <ItemTemplate>
                                   <asp:Label ID="Label6" SortExpression ="DepartureDateTime" CssClass="block" Text='<%# String.Format( "{0:dd-MMM-yyyy}", DataBinder.Eval(Container.DataItem, "(dep_datetime)") ) %>'

                                       runat="server" meta:resourcekey="Label6ResourceKey" />
                               </ItemTemplate>
                           </asp:Repeater>
                       </ItemTemplate>
                       <ItemStyle Font-Bold="False" Width="8em" />
                   </asp:TemplateField>

Ok,

you really should do this when selecting the data originally however if this is not achieveable you can use the following

Solution 1 - Better solution causes less overhead and lets SQL provider/server do the work.
Dim strSQL as string = "SELECT * FROM TravelData ORDER BY DepartureDate ASC, ArrivalDate ASC"



Solution 2:

Private Sub GrabData()
    'Your Data Set
    Dim ReturnedDataSet As New DataSet

    '######
    '   Perform population here of your dataset
    '######

    'Perform The Sort
    Dim tmpDataView As New DataView(ReturnedDataSet.Tables(0)) With {.Sort = "DepartureDate ASC, ArriveDate ASC"}

    'Remove Original and Add
    ReturnedDataSet.Tables.Remove(0) : ReturnedDataSet.Tables.Add(tmpDataView.ToTable)

    MyDataGrid.DataSource = ReturnedDataSet
    MyDataGrid.DataBind()


End Sub