在 DataGridViewTextBoxColumn 中按数字排序 [英] Sorting numerically in a DataGridViewTextBoxColumn
问题描述
这个问题与这两个密切相关(this 和 this) 但我不认为他们给出了令人满意的回答.
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
显然,根据一些线程(例如 这里 和 here),以下应该立即解决问题:
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屋!