DataGridView 自定义排序
我有一个包含 6 列的 DataGridView。
示例:
column1 column2 column3 column4 column5 column6
J6 RES-0112G 123.123 456.456 180 1111
FID2 FIDUCIAL 5.123 -50.005 90 FIDUCIAL
R100 RES-0113G 1.1 -123.123 90 1111
C12 CAP-1234H -99.99 -987.123 45 2222
Q1 CAP-1234Z -99.99 -987.123 45 4444
J3 RES-0112G 123.123 999.999 0 1111
FID1 FIDUCIAL 23.123 23.123 0 FIDUCIAL
F1 CAP-1234 -88.99 -555.111 45 DDDD
C11 CAP-1234Z -123.99 -123.123 270 abc2222
我想按特殊顺序对其进行排序。假设我想按此序列中的最后一个值(第 6 列)对其进行排序:
FIDUCIAL、1111、2222、DDDD、4444
,然后按第二列对其进行排序> 字母数字。 (注意 abc2222
按“2222”而不是“abc”排序)。
因此,更新后的 DataGridView 看起来像这样:(对于 FIDUCIALS
我想按第 1 列而不是第 2 列排序)
column1 column2 column3 column4 column5 column6
FID1 FIDUCIAL 23.123 23.123 0 FIDUCIAL
FID2 FIDUCIAL 5.123 -50.005 90 FIDUCIAL
J6 RES-0112G 123.123 456.456 180 1111
J3 RES-0112G 123.123 999.999 0 1111
R100 RES-0113G 1.1 -123.123 90 1111
C11 CAP-1234C -123.99 -123.123 270 abc2222
C12 CAP-1234H -99.99 -987.123 45 2222
F1 CAP-1234 -88.99 -555.111 45 DDDD
Q1 CAP-1234Z -99.99 -987.123 45 4444
有谁知道如何正确排序?我正在使用 SortableBindingList
I have a DataGridView with 6 columns.
Example:
column1 column2 column3 column4 column5 column6
J6 RES-0112G 123.123 456.456 180 1111
FID2 FIDUCIAL 5.123 -50.005 90 FIDUCIAL
R100 RES-0113G 1.1 -123.123 90 1111
C12 CAP-1234H -99.99 -987.123 45 2222
Q1 CAP-1234Z -99.99 -987.123 45 4444
J3 RES-0112G 123.123 999.999 0 1111
FID1 FIDUCIAL 23.123 23.123 0 FIDUCIAL
F1 CAP-1234 -88.99 -555.111 45 DDDD
C11 CAP-1234Z -123.99 -123.123 270 abc2222
And I would like to sort it in a special order. Let's say I want to sort it by the last value (column 6) in this sequence:
FIDUCIAL, 1111, 2222, DDDD, 4444
AND then sort it secondly by the 2nd column alpha-numerically. (NOTE THE abc2222
sorts by "2222" not "abc").
So the updated DataGridView would look like this: (For the FIDUCIALS
I would like to sort by column 1 instead of column 2)
column1 column2 column3 column4 column5 column6
FID1 FIDUCIAL 23.123 23.123 0 FIDUCIAL
FID2 FIDUCIAL 5.123 -50.005 90 FIDUCIAL
J6 RES-0112G 123.123 456.456 180 1111
J3 RES-0112G 123.123 999.999 0 1111
R100 RES-0113G 1.1 -123.123 90 1111
C11 CAP-1234C -123.99 -123.123 270 abc2222
C12 CAP-1234H -99.99 -987.123 45 2222
F1 CAP-1234 -88.99 -555.111 45 DDDD
Q1 CAP-1234Z -99.99 -987.123 45 4444
Does anyone know how to properly sort this? I am using a SortableBindingList<>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我创建了一个示例代码来向您展示该技术本身。我希望您不会觉得适应您的需求很复杂。基本思想是按所需列进行分组,然后在组内应用自定义排序。我没有掌握column6的排序算法,所以我根据您的示例数据进行了简单的排序。希望有帮助!
I've created a sample code to show you the technique itself. I hope you will not find it complicated to adapt to your needs. The basic idea is that you group by required column and then apply custom sort inside the group. I didn't grasp the sorting algorithm for column6, so I've made a simple sorting based on your sample data. Hope it helps!
SortableBindingList 对您不利,因为它旨在使用一个属性(列)值对数据进行排序。在添加到绑定列表之前,您必须对数据进行排序并禁用排序。您可以使用这样的比较器对数据进行排序。
这是一种编写更加灵活的 Comparer 的方法:
SortableBindingList works against you, because it was designed to sort data using one property (column) values. You will have to sort your data before adding to binding list and disable sorting. You can sort your data with a comparer like this.
Here's a way to write Comparer that is even more flexible:
这是更通用的方法,可以用于像这样的多种情况,
Here is more generic approach, which can be used for multiple cases like this,