Hi, I want to sort my gridview binded to an object datasource fetching business objects. I have already implemented custom paging & now want to implement sorting. Just read this article but there is lot of concatenation going on there with the SQL Query. Any other elegant solution guys ?
+1
A:
How about sorting using .DefaultView
? Following is upon grd_Sorting
event.
DataView dv = dt.DefaultView;//Your datatable, dt.
dv.RowFilter = "";//Set row filter to none.
if ((strSortBy != null) && (strSortAscending != null))
dv.Sort = strSortBy/*Column name*/ + " " + strSortAscending /*ASC, for instance.*/;
grd.DataSource = dv;
grd.DataBind();
KMan
2010-07-19 07:06:14
Hi my object data source returns List<T> with custom business objects. Would this solution work with that ? Also I am using custom paged data (like 10 objects per gridview page)
Popo
2010-07-19 07:27:28
@Popo: In that case, I would implement an IComparable for my custom object. See http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-list-sorting
KMan
2010-07-19 07:59:29
Thanks for the reply. But, that would mean only sorting 10 records and not all records from the database (the query only fetches 10 rows per gridview page so sorting them isn't what Ia m looking for). I think its only possible on database level.
Popo
2010-07-19 10:11:23