Using business objects as a DataSource is quite common these days. Here, rather than communicating directly with the data provider, the client control communicates indirectly through the data cache object. However, it is important to note the fact that the interfaces implemented by the data provider for the benefit of the data cache are different from those provided by the data cache to its clients. Else, we lose few functionality, which considering the flexibility of the C1Flexgrid, is not a pleasant affair. Quoting an example - recently, there was a user who used C1Flexgrid bound to a BindingList. Now, everything else worked good; except Sorting**. Whereas, C1Flexgrid bound to an MS-Access, or a SQL datasource reflects sort because these DataSources support sorting. A BindingList doesn't provide a base implementation of sorting. To implement sorting with a BindingList, we need to sub-class it and override** certain methods (ApplySortCore, RemoveSortCore, SupportsSortingCore, SortDirectionCore etc). Entire code implementation is too long to be shown here; refer the attached sample(s) for complete implementation. Following code snippet create couple of Classes to be used during implementation:
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;
}
}
}
Given code snippet populate objects for the classes described above. Finally, these objects need to be wrapped to the subClassed BindingList and bound to the grid.
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 is now bound to an object of a sortable BindingList. The grid, however, still doesn't display sort-glyphs. To make it do so, subscribe to its AfterSort event.
this.c1FlexGrid1.AfterSort += (s1, e1) =>
{
var flex = s1 as C1.Win.C1FlexGrid.C1FlexGrid;
flex.Invalidate();
flex.ShowSortAt(e1.Order, e1.Col);
};