在DataGridViewTextBoxColumn中按数字排序 [英] Sorting numerically in a DataGridViewTextBoxColumn

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

问题描述

这个问题与这两个问题密切相关(这个这个),但我认为它们并不能令人满意答案.

This question is closely related to these two (this and this) but I don't think they give a satisfying answer.

我有一个DataGridView(即一个表),其中有几列(DataGridViewTextBoxColumn),它们的数据类型不同:字符串,整数和浮点数.当我单击它们各自的标题时,应根据它们的类型对它们进行排序:按字母顺序排列的字符串和按数字顺序排列的数值.简而言之,我有以下代码:

I have a DataGridView (i.e. a table) with several columns (DataGridViewTextBoxColumn) of different data types: string, integers and floats. When I click on their respective header, each should be sorted according to their type: string alphabetically and numerical values numerically. I have, simply put, the following code:

private System.Windows.Forms.DataGridView grid;
private System.Windows.Forms.DataGridViewTextBoxColumn stringColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn doubleColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn intColumn;


stringColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
doubleColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
intColumn    = new System.Windows.Forms.DataGridViewTextBoxColumn();

grid.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        stringColumn,
        doubleColumn,
        intColumn});

但是,由于默认表示形式为string,所以数值也会按字母顺序排序,例如:

However, since the default representation is string, the numerical values also get sorted alphabetically, for example like this:

1, 11, 2, 3, 42, 5

显然,作为解决此问题的一种简便方法,例如某些线程(例如此处),以下应立即解决该问题:

Apparently, as an easy way of getting around this, according some threads (e.g. here and here), the following should work immediately solve the problem:

doubleColumn.ValueType = typeof(double);
intColumn.ValueType = typeof(int);

但是,该解决方案根本无法在我的项目中使用:值仍然按字母顺序排序.所以问题是:为什么不呢?在调试器中,我可以看到值类型实际上已更改为(在double情况下)为System.Double,因此至少发生了一些事情.但是,如何在不编写我自己的分类器的情况下确保它实际对它进行分类呢?

However, this solution simply doesn't work in my project: values are still sorted alphabetically. So the question is: why not? In the Debugger, I could see that the value type actually changed to (in the double case) System.Double, so something is happening at least. But how can I make sure that it actually sorts it accordingly, without writing my own sorter?

推荐答案

您可以处理事件SortCompare来更改排序方式,如下所示:

You can handle the event SortCompare to change how the sorting is done, like this:

private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e) {
    //Suppose your interested column has index 1
    if (e.Column.Index == 1){
       e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
       e.Handled = true;//pass by the default sorting
     }
}

注意:上面的代码假设您的单元格值可以转换为int .

您说您的DataGridView没有分配DataSource,这意味着您手动Add行,所以我认为您应该为单元格使用numeric值而不是string.这将使排序工作如您所愿.

You said your DataGridView doesn't have DataSource assigned, that means you Add the rows manually, so I think you should use numeric values instead of string for your cells. That would make the sorting work as you want.

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

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