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

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

问题描述

我有一个datagridview.It与数字字符串列未绑定,我想对它进行排序号明智的我用

  colid.ValueType = typeof运算(INT);
grid.Sort(colid,ListSortDirection.Descending);

但像各种字符串如:

  11
12
23
7
80
81

而预期为

  7
11
12
23
80
81


解决方案

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

 私人无效customSortCompare(对象发件人,DataGridViewSortCompareEventArgs E)
{
    int类型的= int.Parse(e.CellValue1.ToString()),B = int.Parse(e.CellValue2.ToString());    //如果单元格的值已经是一个整数,只投它,而不是解析    e.SortResult = a.CompareTo(B);    e.Handled =真实的;
}...
yourGridview.SortCompare + = customSortCompare;
...

我没有检查其是否正常工作,但你的想法...;)

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

while the expected is

7
11
12
23
80
81

解决方案

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

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

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