Grid with sorting
I'm writing my own gridview control. The problem is sorting. What is best practic to do that.
I can write function like that:
public object GetDataForGrid(int currentPage, int rowsPerPage, string sortColumnName, string sortDirection)
{
if(sortColumnName="Date" && sortDirection=="ASC")
{
return Detalizations.OrderBy(x => x.Date).Skip(currentPage*rowsPerPage).ToList();
}
.....
//And over 9000 other branches
}
But ofcourse these solution is not optimal and looks oververbose. But ho开发者_运维知识库w i can do it right?
Maybe reflection could help me?
what don't you use the GridView.Sort Method which is made for that scope
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort.aspx
have also a look at:
http://dotnetslackers.com/articles/gridview/Custom-GridView-with-Paging-and-Filtering.aspx#Custom_Filtering
another simple example
https://web.archive.org/web/20210323170551/http://www.4guysfromrolla.com/articles/012308-1.aspx
If you're extending the GridView from the BCL you should look at the builtin Sort. Otherwise, when the sort is changed get the Comparer for the datatype stored in that column (look at Comparer(T).Default), store it someplace, and apply the initial OrderBy to your rows.
Then when a row is updated or inserted, relocate that row based on the comparer that is currently set.
精彩评论