2017-09-01 35 views
0

排序,我想使用自定义的排序Infgragistics。 我读过我可以使用IComparer。风俗的Infragistics

我有一个UltraGridColumnd与字符串数据类型绑定。我希望通过另一列的数据类型进行排序,这是一个很长的数据类型。

可能吗?

回答

2

是的,这是可能的,并且可以使用IComparer接口完全实现。每个UltraGrid列都有SortComparer属性,可以在其上分配实现IComparer接口的对象。正如有关SortComparer属性的文档中所述:

用于执行自定义排序比较时的属性,此列排序为 此列。在IComparer 的Compare方法中传递的值将是两个UltraGridCell对象。

这是关于您的方案的代码片段,因为比较值来自另一列。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     ultraGrid1.DataSource = InitializeDataSource(10); 
     ultraGrid1.DisplayLayout.Override.HeaderClickAction = HeaderClickAction.SortMulti; 
     ultraGrid1.DisplayLayout.Bands[0].Columns[0].SortComparer = new CustomComparer(); 
    } 

    private DataTable InitializeDataSource(int rows) 
    { 
     DataTable table = new DataTable(); 

     table.Columns.Add("String Column", typeof(string)); 
     table.Columns.Add("Long Column", typeof(long)); 

     for (int index = 0; index < rows; index++) 
     { 
      table.Rows.Add(new object[] { "Text", index }); 
     } 

     return table; 
    } 
} 

public class CustomComparer : IComparer 
{ 
    public int Compare(object x, object y) 
    { 
     var valueColumn = "Long Column"; 
     var firstCell = x as UltraGridCell; 
     var secondCell = y as UltraGridCell; 

     var firstCellValue = (long)firstCell.Row.Cells[valueColumn].Value; 
     var secondCellValue = (long)secondCell.Row.Cells[valueColumn].Value; 

     if (firstCellValue == secondCellValue) 
     { 
      return 0; 
     } 
     else if (firstCellValue > secondCellValue) 
     { 
      return -1; 
     } 
     else 
     { 
      return 1; 
     } 
    } 
}