Mat*_*agé 7 asp.net sorting gridview
我需要对需要复杂排序的GridView(Not DataGridView,它不是同一件事)进行排序,我不知道它是如何完成的.我有两列,一列包含日期,另一列包含优先级.
如果日期小于或等于今天的日期.我想首先按优先顺序排序,然后按日期排序.如果日期大于今天的日期,我想先按日期排序,然后按优先顺序排序.
假设totay是2010年1月13日,这种方式订购的一些数据可能看起来像这样
date priority Explanation
----------- -------- --------------------------------------------
2010-jan-13 3 This comes first because it has the higer priority of all items whith a date <= today
2010-jan-12 2 Since this items and the next have the same priority they're ordered by date
2010-jan-13 2
2010-jan-14 5 This item and the followings have date > today, so they are ordered by date then by priority.
2010-jan-14 0
2010-jan-15 5
2010-jan-16 5
Run Code Online (Sandbox Code Playgroud)
无论如何,还是通过手动或通过传递比较仿函数来对网格视图进行排序?
编辑:数据源是DataTable.
您需要对数据源进行排序,而不是对 GridView 进行排序。(告诉我们您的数据源,我们可以提供帮助。是 DataTable、SqlDataSource 还是业务对象?)
来自http://msdn.microsoft.com/en-us/library/hwf94875.aspx上的“在 GridView Web 服务器控件中对数据进行排序”。
如果默认排序行为不能满足您的要求,您可以自定义网格的排序行为。自定义排序的基本技术是处理排序事件。在处理程序中,您可以执行以下操作:
自定义传递到数据源控件的排序表达式。默认情况下,排序表达式是单个列的名称。您可以在事件处理程序中修改排序表达式。例如,如果要按两列排序,则可以创建包含这两列的排序表达式。然后,您可以将修改后的排序表达式传递到数据源控件。有关详细信息,请参阅 GridViewSortEventArgs..::.SortExpression 属性。
创建您自己的排序逻辑。例如,如果您使用的数据源不支持排序,您可以在自己的代码中执行排序,然后将网格绑定到排序后的数据。
Dim oDataSet As DataSet = GatherDataSet()
Dim oDataTable As DataTable = oDataSet.Tables(0)
Dim oSort1 As New DataView(oDataTable, "Date > #2010/01/13#", "Date, Priority", DataViewRowState.CurrentRows)
Dim oSort2 As New DataView(oDataTable, "Date <= #2010/01/13#", "Priority, Date", DataViewRowState.CurrentRows)
Dim oGridDataTable As DataTable
oGridDataTable = oSort1.ToTable
oGridDataTable.Merge(oSort2.ToTable)
oGridView.DataSource = oGridDataTable
'...
'you can then merge any changes back into the data set, if needed
oDataSet.Merge(oGridDataTable)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5189 次 |
最近记录: |