我有两个问题:
有没有办法按列对
DataGridView
进行排序 - 我想使用代码来执行此操作,以便我的方法之一可以对DataGridView
进行排序。 (与用户点击列标题相同的效果,但没有点击。)当我对具有
decimal
值(double
类型)的列进行排序时,排序认为 9.99 大于 24.99 - 有什么方法可以防止这种情况发生排序行为的类型?
更新:由于评论中的链接,问题 1 已解决。
我还找到了问题 2 的问题原因:我的专栏中的值为 (double)price + "€"
。我尝试删除 + "€"
并开始按我想要的方式排序。
现在我希望它能够排序,同时以某种方式保留 €。
最佳答案
由于 stuartd 正确地回答了第一个问题,我们将重点关注第二个问题:
When I sort a column with
decimal
values (of typedouble
) the sorting believes that 9.99 is larger than 24.99 - is there any way to prevent this type of sorting behaviour?
正如您所注意到的,删除 + "€"
更正了您的问题,因为使用它该列执行的是 string
比较,而不是预期的 double
比较。您可以使用 CultureInfo 修复此问题将该列格式化为货币列。
CultureInfo ci = new CultureInfo("fr-FR");
this.dataGridView1.Columns[0].DefaultCellStyle.FormatProvider = ci.NumberFormat;
this.dataGridView1.Columns[0].DefaultCellStyle.Format = "c";
关于关于 DataGridView 排序的 C# 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42768938/