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

查看:888
本文介绍了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.

您可能已经看到了但这里 href=\"http://msdn.microsoft.com/en-us/library/ms171608.aspx\">是到MSDN页面的链接上定制DataGridView的排序。正如你所说,他们只能处理文字。

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天全站免登陆