You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows Forms无绑定DataGridView列字母数字排序异常问题咨询

DataGridView混合类型列的自然排序问题

当前基础排序功能对以下列表有效:

001.15
1
2
3
some
string

但添加数字100后,排序结果不符合预期:

001.15
1
100
2
3
some
string

期望100排在3之后。需要排序的“Test ID”列包含字符串、整数和双精度数。

尝试过的方法及问题

  1. 尝试在按钮点击时调用自定义排序方法:
private void SortDVG(object sender, DataGridViewSortCompareEventArgs e)
{
     if (e.Column.Index == 0)
     {
         e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
         e.Handled = true;
     }
 }

按钮点击事件中的调用代码:

SortDVG(dataGridView1, (DataGridViewSortCompareEventArgs)e);

但抛出运行时错误:

System.InvalidCastException: 'Unable to cast object of type 'System.Windows.Forms.MouseEventArgs' to type 'System.Windows.Forms.DataGridViewSortCompareEventArgs'.'

  1. 尝试直接调用DataGridView的Sort方法,这也是产生初始排序结果的代码:
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);

目前无法找到可行的解决方案,希望得到解决方向指引。

内容的提问来源于stack exchange,提问作者mjtrembley

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:50:56