Is it possible to perform custom sorting in a gridview

I need to sort a GridView (not a DataGridView, it's not the same thing), which needs complex sorting, and I don't know how to do this. I have two columns, one of which contains the date, and the other is the priority.

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

Suppose totay is 2010-jan-13, some data arranged in this way might look like this:

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 

Do I need to sort the grid view manually or by passing a comparison function?

edit: The data source is a DataTable.

+7
sorting gridview
source share
4 answers

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

From sorting data into a GridView web server control at http://msdn.microsoft.com/en-us/library/hwf94875.aspx .

Custom sort

If the default sorting behavior is not enough for your requirements, you can customize the grid sorting behavior. The main method for custom sorting is handling the Sort 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 change the sort expression in an 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 GridViewSortEventArgs .. :: Property. SortExpression.

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

Sorting example (not tested and not used LINQ [as intended])

 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) 
+3
source share

Salute Mathieu,

Assuming you are using a DataSource and each element is a class (instead of, for example, datarow), you can implement IComparable for your class. It adds a CompareTo method in which you decide how each element is compared with each other.

 class DatePriority: IComparable { private DateTime date; public DateTime Date { get { return date; } } private int priority; public int Priority { get { return priority; } } public DatePriority(DateTime date, int priority) { this.date = date; this.priority = priority; } public int CompareTo(object obj) { if (obj is DatePriority) { DatePriority comparedDatePriority = obj as DatePriority; // Comparison logic // If the compared elements are today or before today, order by priority in descending order. Same priorities are ordered by date in ascending order // If the compared elements are for the future, order by date in ascending order. Same dates are order by priority in descending order if ((this.Date <= DateTime.Today && comparedDatePriority.Date <= DateTime.Today)) { if (Priority == comparedDatePriority.Priority) return Date.CompareTo(comparedDatePriority.Date); else return -Priority.CompareTo(comparedDatePriority.Priority); } else { if (Date == comparedDatePriority.date) return -Priority.CompareTo(comparedDatePriority.Priority); // Descending order else return Date.CompareTo(comparedDatePriority.Date); } } throw new ArgumentException("Not a DatePriority"); } } 
0
source share

Yes you can do it. This is how you do it.

Assuming you have a GridView that calls the entries from the BusinessLayer method. I will take the UserManager example as a business level proxy class.

  [DataObjectAttribute()] public static class UserManager { [DataObjectMethod(DataObjectMethodType.Select, true)] public static UserCollection GetUsers() { return UserDB.GetAll(); } [DataObjectMethod(DataObjectMethodType.Select, false)] public static UserCollection GetUsers(string sortExpression) { UserCollection users = UserDB.GetAll(); users.Sort(new EntityComparer<User>(sortExpression)); return users; } } 

Look at this line of code in the overloaded GetUsers method.

 users.Sort(new EntityComparer<User>(sortExpression)); 

I wrote a Generic Comparer implementation for my business objects. EntityComparer is just a general class that implements the IComparer interface. You can write your own implementation of Comparer [which implements the IComparer interface] and name it as the code above.

This is what my GridView looks like ..

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" /> <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" /> <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" /> </Columns> </asp:GridView> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetUsers" TypeName="FilePark.BusinessLayer.UserManager"></asp:ObjectDataSource> 

Also note that the GridView will pass sortExpression after the postback and will call the overloaded method.

0
source share

Data Sort Procedure GreenView uses a data source sort procedure. To manually sort the data creation class that implements IBindingListView, implement its sort method as you want and use this class as a DataSource in a DataGridView

-one
source share