如何在winforms的datagridview中将字符串排序为数字 [英] how to sort string as number in datagridview in winforms

查看:27
本文介绍了如何在winforms的datagridview中将字符串排序为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 datagridview 中有带有数字的字符串列.它没有绑定,我想按照我使用的数字对它进行排序

I have string column with numbers in a datagridview.It is not bound, I would like to sort it number wise I used

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

但是类似于字符串,例如:

but is sorts like string eg:

11
12
23
7
80
81

而预期是

7
11
12
23
80
81

推荐答案

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

You can register on the SortCompare event, for example:

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;
...

我没有检查它是否有效,但你明白了......;)

I didn't check if that works, but you get the idea... ;)

这篇关于如何在winforms的datagridview中将字符串排序为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆