← 返回所有博客文章

近期,使用业务对象作为数据源是十分常见的案例。在这里,不是直接与 Data provider 通信操作数据。C1FlexGrid 直接与客户端缓存数据对象交互。但是,通过 Data Provider 提供的数据缓存和存储在客户端的缓存有所不同。使用后者,我们将无法使用 C1Flexgrid 排序特性。这是不尽如人意的。

引用实际工作中案例-最近有用户使用 C1FlexGrid 绑定 BindingList。但是,排序功能无法正常使用。然而,MS-Access SQL 绑定到 C1FlexGrid 时,支持排序功能,因为这两种数据源类型本身支持排序。BindingList 没有提供排序接口。为了实现该功能,我们可以继承该类,重载 (ApplySortCore, RemoveSortCore, SupportsSortingCore, SortDirectionCore 等)方法。

下面代码为基类:

public class Sales 
{ 
public Sales() 
{ 
  SaleDate = DateTime.Now; 
} 
public SortedBindingList<Book> SaleDetails { get; set; } 
public string Dealer { get; set; } 
public string Customer { get; set; } 
public DateTime SaleDate { get; set; } 
public decimal TotalAmount 
{ 
  get 
  { 
   return SaleDetails.Sum(a => a.TotalAmount); 
  } 
} 
} 
public class Book 
{ 
public string Title { get; set; } 
public int Quantity { get; set; } 
public decimal Cost { get; set; } 
public decimal TotalAmount 
{ 
  get 
  { 
   return Cost * Quantity; 
  } 
} 
}

最后,创建商业对象列表,用于绑定 C1FlexGrid:

var sales = new[] { 
  new Sales(){ 
      Customer = "John Smith", 
      SaleDate = new DateTime(2008,1,1), 
      Dealer = "James Franco", 
      SaleDetails = new SortedBindingList<Book>(){ 
        new Book(){ 
          Title = "Pride And Prejudice", 
          Quantity = 1, 
          Cost = 25 
        }, 
        new Book(){ 
        Title = "Jane Eyer", 
        Quantity = 2, 
        Cost = 35 
        }, 
        new Book(){ 
        Title = "Wuthering Heights", 
        Quantity = 1, 
        Cost = 55 
        } 
      } 
    }, 
   new Sales(){ 
     Customer = "John Ayers", 
     SaleDate = new DateTime(2008,1,2), 
     Dealer = "Tom Cruise", 
     SaleDetails = new SortedBindingList<Book>(){ 
       new Book(){ 
        Title = "Emma", 
        Quantity = 1, 
        Cost = 80 
       }, 
       new Book(){ 
       Title = "Sense And Sensibility", 
       Quantity = 5, 
       Cost = 100 
       }, 
       new Book(){ 
       Title = "Romeo And Juliet", 
       Quantity = 3, 
       Cost = 50 
       } 
     } 
   } 
}; 
//BindingList set as grid's DataSource 
this.c1FlexGrid1.DataSource = new SortedBindingList<Sales>(sales);

现在,C1FlexGrid 已经绑定了 BindingList。以下代码用于显示排序指示器:

this.c1FlexGrid1.AfterSort += (s1, e1) => 
{ 
  var flex = s1 as C1.Win.C1FlexGrid.C1FlexGrid; 
  flex.Invalidate(); 
  flex.ShowSortAt(e1.Order, e1.Col); 
};

 

Download CS Sample
Download VB Sample