如何在Datagrid中对来自数据库的项进行排序 [英] How Can I Sort item in Datagrid that is coming from database

查看:37
本文介绍了如何在Datagrid中对来自数据库的项进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <   asp:TemplateField     HeaderText   = 出发日期    meta:resourcekey   =  TemplateFieldResource7       >  
< ItemTemplate >
< asp:Repeater ID = repSector runat = 服务器 DataSource =' <%#CType(Container.DataItem,System.Data.DataRowView).Row.GetChildRows( Booking_Part_TO_Sector_FK%> ' >
< ItemTemplate >
< asp:标签 ID = Label6 SortExpression = < span class =code-keyword> DepartureDateTime
CssClass = block 文字 =' <%#< span class =code-sdkkeyword> 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 宽度 = 8em / >
< / asp:TemplateField >

解决方案

好的,



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



解决方案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(< span class =code-digit> 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


这篇关于如何在Datagrid中对来自数据库的项进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆