gpt4 book ai didi

c# - 如何在 winforms 的 datagridview 中将字符串排序为数字

转载 作者:可可西里 更新时间:2023-11-01 03:14:24 25 4
gpt4 key购买 nike

我在 datagridview 中有带数字的字符串列。它没有绑定(bind),我想按数字对它进行排序

colid.ValueType = typeof(int);
grid.Sort(colid, ListSortDirection.Descending);

但有点像字符串,例如:

11
12
23
7
80
81

虽然预期是

7
11
12
23
80
81

最佳答案

可以在SortCompare事件上注册,例如:

private void customSortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
int a = int.Parse(e.CellValue1.ToString()), b = int.Parse(e.CellValue2.ToString());

// If the cell value is already an integer, just cast it instead of parsing

e.SortResult = a.CompareTo(b);

e.Handled = true;
}

...
yourGridview.SortCompare += customSortCompare;
...

我没有检查这是否有效,但你明白我的想法了……;)

关于c# - 如何在 winforms 的 datagridview 中将字符串排序为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2674670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com