ComponentOne FlexGrid for WinForms自定义排序

时间:2024-11-07 09:57:43

1、使用场景:排序尺寸这里有一些尺寸需要排序larger或smaller,这些尺寸参数是:XS,S,M,L,XL和XXL。你可以需要比较尺寸,这个时候就需要就行自定义。需求如图

ComponentOne FlexGrid for WinForms自定义排序

2、IComparer InterfaceSystem.Collections.IComparer接口暴露了一个方法叫做:System.Collections.IComparer.Compare(System.Object,System.Object) 。它有2个对象作为参数(在这个对象里,是行对象),然后返回-1,0或是1.这个接口通过Sort方法提供自定义排序。IComparer允许重用,重写比较,这对需要额外比较的需求的很有用

3、对C1FlexGrid应用自定义排序通过C1FlexGrid的Sort构造方法,以及连个IComparer接口,就可以对行的组进行特定的比较。为了对尺寸应用排序,从IComparer接口继承。这个类会做比较重新做排序。

4、代码参考:private void sort_Click(object sender, EventArgs e){c1FlexGrid1.Sort(new MyComparer(c1FlexGrid1)); // Perform a custom sort on the grid.}public class MyComparer: IComparer{C1FlexGrid cfg;public MyComparer(C1FlexGrid cfg){this.cfg = cfg;}public int Compare(object r1,object r2){int cmp = 0;string s1 = cfg[((Row)r1).Index, 3].ToString();string s2 = cfg[((Row)r2).Index, 3].ToString();if(s1=="XS"){if (s2 == "XS")cmp = 0;elsecmp = -1;}else if (s1 == "XXL"){if (s2 == "XXL")cmp = 0;elsecmp = 1;}else if (s1 == "S"){if (s2 == "XS")cmp = 1;else if (s2 == "S")cmp = 0;elsecmp = -1;}else if (s1 == "L"){if (s2 == "XS")cmp = 1;else if (s2 == "S")cmp = 1;else if (s2 == "M")cmp = 1;else if (s2 == "L")cmp = 0;elsecmp = -1;}else if (s1 == "M"){if (s2 == "XS")cmp = 1;else if (s2 == "S")cmp = 1;else if (s2 == "M")cmp = 0;elsecmp = -1;}else if (s1 == "XL"){if (s2 == "XXL")cmp = -1;else if (s2 == "XL")cmp = 0;elsecmp = 1;}return cmp;}}

© 2025 手抄报圈
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com