Чтобы заставить ASP.NET GridView правильно сортировать, когда несколько столбцов перечислены в спецификации сортировки BoundField, вам необходимо привязать событие OnSorting сетки к этой функции:
protected void gridViewSorting(object sender, GridViewSortEventArgs e)
{
//
// This odd function permits GridView objects to sort on multiple columns
// Without this function, a GridView object does not sort correctly when multiple
// columns are named in its sort specification.
//
if (!(sender is GridView))
{
return;
}
if (!e.SortExpression.Contains(','))
{
return;
}
GridView gv = sender as GridView;
//
// Find the column that is to become the basis of the sort
//
foreach (DataControlField dc in gv.Columns)
{
String fieldSortExprClean = dc.SortExpression.Replace(" DESC", "");
fieldSortExprClean = fieldSortExprClean.Replace(" ASC", "");
String eventSortExprClean = e.SortExpression.Replace(" DESC", "");
eventSortExprClean = eventSortExprClean.Replace(" ASC", "");
if (fieldSortExprClean == eventSortExprClean)
{
if (e.SortDirection == SortDirection.Ascending)
{
dc.SortExpression = fieldSortExprClean.Replace(",", " ASC,");
e.SortExpression = fieldSortExprClean.Replace(",", " ASC,");
}
else
{
dc.SortExpression = fieldSortExprClean.Replace(",", " DESC,");
e.SortExpression = fieldSortExprClean.Replace(",", " DESC,");
}
}
}
}