C# 在DataGridViewTextBoxColumn中进行数字排序
这个问题与这两个(和)密切相关,但我认为他们没有给出令人满意的答案 我有一个C# 在DataGridViewTextBoxColumn中进行数字排序,c#,winforms,sorting,datagridview,C#,Winforms,Sorting,Datagridview,这个问题与这两个(和)密切相关,但我认为他们没有给出令人满意的答案 我有一个DataGridView(即一个表),其中有几个列(DataGridViewTextBoxColumn)具有不同的数据类型:字符串、整数和浮点。当我单击它们各自的标题时,每个标题都应该根据它们的类型进行排序:按字母顺序排列的字符串和按数字顺序排列的数值。简单地说,我有以下代码: private System.Windows.Forms.DataGridView grid; private System.Windows.F
DataGridView
(即一个表),其中有几个列(DataGridViewTextBoxColumn
)具有不同的数据类型:字符串、整数和浮点。当我单击它们各自的标题时,每个标题都应该根据它们的类型进行排序:按字母顺序排列的字符串和按数字顺序排列的数值。简单地说,我有以下代码:
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});
但是,由于默认表示法为字符串
,因此数值也会按字母顺序排序,例如:
1, 11, 2, 3, 42, 5
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
}
}
显然,作为解决此问题的一种简单方法,根据一些线程(例如和),以下操作应能立即解决问题:
doubleColumn.ValueType = typeof(double);
intColumn.ValueType = typeof(int);
然而,这个解决方案在我的项目中根本不起作用:值仍然是按字母顺序排序的。所以问题是:为什么不呢?在调试器中,我可以看到值类型实际上更改为(在
double
案例中)System.double
,因此至少发生了一些事情。但是,我如何确保它在不编写自己的分类器的情况下对其进行相应的排序呢?您可以处理事件SortCompare
来更改排序方式,如下所示:
1, 11, 2, 3, 42, 5
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
,这意味着您需要手动添加行,因此我认为您应该为单元格使用numeric
值,而不是string
。这将使排序工作正常。您可以处理事件SortCompare
来更改排序方式,如下所示:
1, 11, 2, 3, 42, 5
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
,这意味着您需要手动添加行,因此我认为您应该为单元格使用numeric
值,而不是string
。这将使排序工作正常。将列从string
更改为int32
可能会有所帮助:
for (int i = 0; i < tableDataGridView.Rows.Count; i++) {
DateTime dt = Convert.ToDateTime(tableDataGridView.Rows[i].Cells[9].Value.ToString());
DateTime dtnow = DateTime.Now;
TimeSpan ts = dtnow.Subtract(dt);
tableDataGridView.Rows[i].Cells[1].Value = Convert.ToInt32( ts.Days.ToString());
}
tableDataGridView.Sort(tableDataGridView.Columns[1], ListSortDirection.Descending);
for(int i=0;i
对我来说,这很有效。我希望这会有所帮助。将列从
string
更改为int32
可能会有所帮助:
for (int i = 0; i < tableDataGridView.Rows.Count; i++) {
DateTime dt = Convert.ToDateTime(tableDataGridView.Rows[i].Cells[9].Value.ToString());
DateTime dtnow = DateTime.Now;
TimeSpan ts = dtnow.Subtract(dt);
tableDataGridView.Rows[i].Cells[1].Value = Convert.ToInt32( ts.Days.ToString());
}
tableDataGridView.Sort(tableDataGridView.Columns[1], ListSortDirection.Descending);
for(int i=0;i
对我来说,这很有效。我希望它会有所帮助。如果您使用的是
数据表
,则必须在数据列
上设置数据类型。在DataGridViewTextBoxColumn
上设置ValueType
将没有帮助
您可以在创建时进行设置:
table.Columns.Add("Number", typeof(int));
如果使用的是数据表
,则必须在数据列
上设置数据类型
。在DataGridViewTextBoxColumn
上设置ValueType
将没有帮助
您可以在创建时进行设置:
table.Columns.Add("Number", typeof(int));
将列ValueType设置为typeof(int)会起作用,只需记住确保将整数放入该列即可。如果其余数据包含字符串,则很容易忘记将数字从字符串转换为int。将列ValueType设置为typeof(int)会起作用,请记住确保将整数放入该列。如果其余数据包含字符串,则很容易忘记将数字从字符串转换为整数。您的网格
数据绑定了吗?我不确定这意味着什么,请您指定一下好吗?我的意思是您的DataGridView
是否有数据源
分配给任何对象?啊哈,谢谢。不,没有。您的网格
数据绑定了吗?我不确定这是什么意思,请您具体说明一下?我的意思是您的数据网格视图
是否有数据源
分配给任何对象?啊哈,谢谢。不,不是。谢谢,这也是一个选择,但这不是问题的真正答案,因为这是在制作你自己的分拣机。我问这个问题的原因是,我将有许多列,这些列的类型可能在运行时决定,每次列更改时分配和更改SortCompare
方法将变得非常乏味。因此获得doubleColumn.ValueType=typeof(double)
工作将是一个主要优势。@Yellow在使用自定义排序器时,您可以添加更多的数据结构来控制,因为在我的代码中,自定义排序器仅应用于索引1
@Yellow处的列。如果您有更复杂的情况,您应该使用DataGridView,并将数据源分配给某个对象,事实上,我从未见过没有数据源的DataGridView,除非它只是临时显示数据,可能是只读的。@Yellow当您向DataGridView
添加行时,只需尝试添加数值
值,而不是字符串
。然后,您的网格将知道如何根据需要对列进行排序。更改SortCompare事件最适合我。我的列中有很多零,希望它们显示为空字符串,所以我使用字符串作为列类型。但在排序时,我希望它们按数字排序。我刚刚加了f