DataGridView十进制不排序 [英] DataGridView decimal not sorting

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

问题描述

好的,我有一个正在被数据绑定的DataGridView,

Alright, I have a DataGridView which is being databound like:

dataGridViewChartOre.AutoGenerateColumns = false;
dataGridViewChartOre.DataSource = xml.GetOreChart();
dataGridViewChartOre.DataMember = "ore";

所有列均已通过UI手动创建.列由1个字符串,10个小数组成.最后一栏是货币.

All columns have been created manually via the UI. And the columns consists of 1 string, 10 decimals. Where the last column is a currency.

现在一次可以在dataGridView中显示货币很快就可以解决,我记得xml的输入始终是一个字符串.使用这种方法:(在此站点的其他位置找到.)

Now getting a currency to show in the dataGridView was quick to solve once, I remembered that the input from xml was always a string. Using this approach: (Found else where on this sites.)

private void dataGridViewChartOre_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        string value = e.Value.ToString();
        decimal d;

        if (decimal.TryParse(value, out d))
        {
            e.Value = d;
        }
    }

现在,要排序的小数似乎是一个更大的问题.我已经尝试了一些建议的解决方案,但是到目前为止都没有奏效.

Now getting, the decimals to sort, seems to of a much bigger problem. I've tried a couple of suggested solutions, however none have worked so far.

  • 使用浮点数而不是小数点.
  • 利用SortCompare//SortResult

现在,它仅按第一个数字对所有小数进行排序,即:

Right now, it sorts all the decimals by its first number only, ie:

8616
8225
785995
7833
78069
773403
750268
74521
738249
714541
70972
and so on

那么如何解决这个问题,如果有人可以向我解释为什么这样做,甚至会更好?

So how to fix this, and even better if somebody could explain to me why it does this ?

此外,在旁注中,如何更改CurrencySymbol?我想在整个程序中使用其他符号,但是,它应该保留常规的美国日期时间格式.

Also, on a side note, how to change CurrencySymbol? I want to use a different symbol accross the whole program, however, it should retain the regular en-US datetime format.

推荐答案

您的DataGridView列类型取决于基础数据的实际类型.您不能更改基础数据的类型,在CellFormatting事件处理程序中更改的只是单元格值.如果更改了单元格值的类型,使其类型与所属列的类型(由基础数据确定)不匹配,则会抛出FormatException.我想您的代码(在CellFormatting事件处理程序中)将在更改单元格值后添加e.FormattingApplied = true;时引发该异常,这实际上会影响格式设置,但您会错过它.)

Your DataGridView Column type depends on the actual type of the underlying data. You can't change the type of the underlying data, what you changed in the CellFormatting event handler is just the cell value. If the cell value type is changed so that its type is not matched with the type of the owning column (which is determined by the underlying data), there will be a FormatException thrown. I guess your code (in CellFormatting event handler) will throw that exception if you add e.FormattingApplied = true; after changing the cell value, that will actually affect the formating but you missed it).

您提到了SortCompare事件,并说它没有帮助,但是我认为您可能没有以正确的方式使用它.我已经尝试过使用该解决方案,并且有效:

You mentioned SortCompare event and said it didn't help, but I think you might not use it in a correct way. I've tried this solution with it and it worked:

private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
   e.SortResult = Compare(e.CellValue1.ToString(), e.CellValue2.ToString());
}
private int Compare(string a, string b)
{
   return decimal.Parse(a).CompareTo(decimal.Parse(b));
}

这篇关于DataGridView十进制不排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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