Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do a custom sort on a GridView

I need to sort a GridView (Not DataGridView, it's not the same thing) that is in need of a complex sort and I don't know how it can be done. I have two column, one containing a date and another containing a priority.

If the date is lesser or equal to today's date. I want to order by priority first, then by date. If the date is greater than today's date, I want to order by date first, then by priority.

Suppose totay is 2010-jan-13, some data ordered this way could look like these

   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

Is there anyway to sort a grid view mannually or by passing a compare functor?

edit : The datasource is a DataTable.

like image 709
Mathieu Pagé Avatar asked Jan 13 '10 16:01

Mathieu Pagé


1 Answers

You need to sort your data source, not the GridView. (Tell us your data source and we can help. Is it a DataTable, SqlDataSource, business objects?)

From Sorting Data in a GridView Web Server Control at http://msdn.microsoft.com/en-us/library/hwf94875.aspx.

Custom Sorting

If the default sort behavior is not adequate for your requirements, you can customize the grid's sorting behavior. The basic technique for custom sorting is to handle the Sorting event. In the handler, you can do the following:

  • Customize the sort expression that is passed to the data source control. By default, the sort expression is the name of a single column. You can modify the sort expression in the event handler. For example, if you want to sort by two columns, you can create a sort expression that includes both. You can then pass the modified sort expression to the data source control. For more information, see the GridViewSortEventArgs..::.SortExpression property.

  • Create your own sorting logic. For example, if you are working with a data source that does not support sorting, you can perform the sort in your own code and then bind the grid to the sorted data.

Sort Example (Not Tested and Not Using LINQ [on purpose])

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)
like image 157
AMissico Avatar answered Oct 12 '22 08:10

AMissico