C1FlexGrid: 对绑定的数据对象排序

发布时间:2013/03/14 00:03 发布者:iceman

返回博客中心

近期,使用业务对象作为数据源是十分常见的案例。在这里,不是直接与 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


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网