2011-02-02 51 views

回答

1

Here是以前已回答的问题。

对于实际的排序,你会打电话

collectionOfObjects.OrderBy(x => x.PropertyToSortOn); 

你可以使用一个开关来改变基于什么是传递到通过ARGS的方法进行排序的。所以它会看起来更像这个

switch(propertyName) 
{ 
    case "property1": 
    collectionOfObjects.OrderBy(x => x.PropertyToSortOn); 
    break; 
    case "property2": 
    collectionOfObjects.OrderBy(x => x.OtherPropertyToSortOn); 
    break; 

    ... 

} 

希望这有助于! :)

+0

我不明白答案,我已经读完了。 – Kovu 2011-02-02 16:02:21

0

如果对您来说更容易,为什么不尝试从存储过程或查询中对其进行排序。 也许不是optimun解决方案,但它可能更容易。

编辑

如果你想在GridView控件做programattically,看看下面的代码:

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
     { 
      GridView1.PageIndex = e.NewPageIndex; 
      GridView1.DataBind(); 
     } 


     protected void gridView_Sorting(object sender, GridViewSortEventArgs e) 
     { 
      DataTable dtSortTable = GridView1.DataSource as DataTable; 
      if (dtSortTable != null) 
      { 
       DataView dvSortedView = new DataView(dtSortTable); 
       dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(e.SortDirection); 
       GridView1.DataSource = dvSortedView; 
       GridView1.DataBind(); 
      } 
     } 

     private string getSortDirectionString(SortDirection sortDireciton) 
     { 
      string newSortDirection = String.Empty; 
      if (sortDireciton == SortDirection.Ascending) 
      { 
       newSortDirection = "ASC"; 
      } 
      else 
      { 
       newSortDirection = "DESC"; 
      } 
      return newSortDirection; 
     }