Winform DatagridView数字列排序 [英] Winform DatagridView Numeric Column Sorting

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

问题描述

我正在使用一个简单的DataGridView来保存一堆数据(有趣的是)。

I am using just a simple DataGridView to hold a bunch of data (Funny that).

我在特定列中有小数位。但是,当订购该十进制列时,它不正确地排序。例如:

I have decimals in a particular column. But when it comes to ordering by that decimal column, it orders it incorrectly. For example :

起始订单可能是:


  • 0.56

  • 3.45

  • 500.89

  • 20078.90

  • 1.56

  • 100.29

  • 2.39

  • 0.56
  • 3.45
  • 500.89
  • 20078.90
  • 1.56
  • 100.29
  • 2.39

结束顺序将是:


  • 0.56

  • 100.29

  • 1.56

  • 20078.90

  • 2.39

  • 3.45

  • 500.89

  • 0.56
  • 100.29
  • 1.56
  • 20078.90
  • 2.39
  • 3.45
  • 500.89

如您所见,它从第一个数字开始订购。然后以这种方式订购。

As you can see, it orders it starting from the first number. And then orders it in this way.

我以为可能将列设置为不同的ColumnType,可能会自动执行。但是没有数字或十进制列类型。

I thought possibly I could set the column to a different "ColumnType" and that may automatically do it. But there is no "Numeric" or "Decimal" column types.

我在MSDN上查找问题,我可以找到我可以在DataGridView上使用的排序方法。但这个解释有点在我头上,而且这些例子并没有使用数字,只是文字,所以我看不出我应该如何切换。

I was on MSDN looking up the issue, and I could find the "sort" method that I can use on the DataGridView. But the explanation was a bit over my head, and the examples didn't use numbers, only text so I couldnt see how I was supposed to switch things up.

任何帮助将不胜感激。

Any help would be much appreciated.

推荐答案

您可以通过在DataGridView上为SortCompare事件添加一个处理程序来解决此问题,具有以下代码:

You can solve this by adding a handler for the SortCompare event on the DataGridView with the following code:

private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    if (e.Column.Index == 0)
    {
        if (double.Parse(e.CellValue1.ToString()) > double.Parse(e.CellValue2.ToString()))
        {
            e.SortResult = 1;
        }
        else if (double.Parse(e.CellValue1.ToString()) < double.Parse(e.CellValue2.ToString()))
        {
            e.SortResult = -1;
        }             
        else
        {
            e.SortResult = 0;
        }
        e.Handled = true;
   }
}

从MSDN中有对SortResult值的描述:

From MSDN there is this description of the SortResult values:


如果第一个单元格
在第二个单元格之前排序,则小于零;零
如果第一个单元格和第二个单元格具有
等价值;大于零
如果第二个单元格将在第一个单元格之前排序

Less than zero if the first cell will be sorted before the second cell; zero if the first cell and second cell have equivalent values; greater than zero if the second cell will be sorted before the first cell.

请注意,在我的测试床唯一的数字列是第一个(索引为0),这就是为什么我有对列索引的检查。

Note that in my test bed the only numeric column was the first (with index 0) so that is why I have the check on the column index.

另外,根据你的需要和数据您可能需要改进我的代码 - 例如,如果您的列中有非数字数据,我的代码将抛出异常。

Also, depending on your needs and data you may want to refine my code - for example, my code will throw an exception if for some reason you have non numeric data in your column.

您可能已经看过它,但 here 是指向定制DataGridView排序的MSDN页面的链接。正如你所说,他们只处理文字。

You have probably seen it, but here is a link to the MSDN page on customising the DataGridView sorting. As you say, they only deal with text.

这篇关于Winform DatagridView数字列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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