Windows Forms无绑定DataGridView列字母数字排序异常问题咨询
DataGridView混合类型列的自然排序问题
当前基础排序功能对以下列表有效:
001.15 1 2 3 some string
但添加数字100后,排序结果不符合预期:
001.15 1 100 2 3 some string
期望100排在3之后。需要排序的“Test ID”列包含字符串、整数和双精度数。
尝试过的方法及问题
- 尝试在按钮点击时调用自定义排序方法:
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'.'
- 尝试直接调用DataGridView的Sort方法,这也是产生初始排序结果的代码:
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
目前无法找到可行的解决方案,希望得到解决方向指引。
内容的提问来源于stack exchange,提问作者mjtrembley
相关产品推荐
相关产品推荐

