如何在DataGridView中将字符串作为数字排序 [英] How to sort string as numbers in DataGridView
问题描述
示例:
Dgv如下排序:
- FS 752/08/2014
- FS 752/06/2015
- FS 751/12/2013
- FS 751/08 / 2014
- FS 751/06/2015
它应该是这样的: p>
- FS 752/06/2015
- FS 751/06/2015
- FS 752/08 / 2014
- FS 751/08/2014
- FS 751/12/2013
如何实现?
正如您所说,您没有使用 DataSource
然后你可以尝试使用 SortCompare
事件。
用户单击列更改排序顺序,或者当您编程调用 排序
功能。
这是一个基本的例子。我做出假设,你想排序年份(?)然后是月(?)然后第一个数字:
private void dataGridView1_SortCompare(object sender,DataGridViewSortCompareEventArgs e)
{
//检查是否按特殊列排序。
if(myDataGridView.Columns.Contains(My_Column)&& e.Column == myDataGridView.Columns [My_Column])
{
//解析特殊值(如果需要,则添加验证)。
string [] parts1 = e.CellValue1.ToString()。Trim()。Split('/');
int a1 = int.Parse(parts1 [0] .Split('')[2]);
int b1 = int.Parse(parts1 [1]);
int c1 = int.Parse(parts1 [2]);
string [] parts2 = e.CellValue2.ToString()。Trim()。Split('/');
int a2 = int.Parse(parts2 [0] .Split('')[2]);
int b2 = int.Parse(parts2 [1]);
int c2 = int.Parse(parts2 [2]);
//根据需要比较每个值。
//首先比较最后一个值(年?)
e.SortResult = c1.CompareTo(c2);
//如果相等,则比较第二个值(month?)
if(e.SortResult == 0)
e.SortResult = b1.CompareTo(b2);
//如果仍然相等,则比较第一个值
if(e.SortResult == 0)
e.SortResult = a1.CompareTo(a2);
}
}
注意:如果您无法确保每个单元格中始终都有有效的格式,那么您将需要为此事件添加验证逻辑。例如,使用 int.TryParse()
并检查 null
值,并验证<$ c的长度$ c> string split。
I have string column with specific numbers in a datagridview. It's not bound. I would like to sort it correctly. Default DataGridView sorting doesn't sort it correct.
Example: Dgv sorts it like this:
- FS 752/08/2014
- FS 752/06/2015
- FS 751/12/2013
- FS 751/08/2014
- FS 751/06/2015
And it should be like this:
- FS 752/06/2015
- FS 751/06/2015
- FS 752/08/2014
- FS 751/08/2014
- FS 751/12/2013
How can I achieve this?
As you have stated you are not using a DataSource
then you could try using the SortCompare
event.
This event will be called when the user clicks a column to change the sort order, or when you programatically call the Sort
function.
Here is a basic example. I am making assumptions that you want to sort be year(?) then month(?) then the first number:
private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
// Check if we are sorting by the special column.
if (myDataGridView.Columns.Contains("My_Column") && e.Column == myDataGridView.Columns["My_Column"])
{
// Parse the special values (add validation if required).
string[] parts1 = e.CellValue1.ToString().Trim().Split('/');
int a1 = int.Parse(parts1[0].Split(' ')[2]);
int b1 = int.Parse(parts1[1]);
int c1 = int.Parse(parts1[2]);
string[] parts2 = e.CellValue2.ToString().Trim().Split('/');
int a2 = int.Parse(parts2[0].Split(' ')[2]);
int b2 = int.Parse(parts2[1]);
int c2 = int.Parse(parts2[2]);
// Compare each value as required.
// First compare the last value (year?)
e.SortResult = c1.CompareTo(c2);
// If equal, then compare second value (month?)
if(e.SortResult == 0)
e.SortResult = b1.CompareTo(b2);
// Finally if still equal, then compare first value
if(e.SortResult == 0)
e.SortResult = a1.CompareTo(a2);
}
}
NOTE: If you cannot ensure you will always have a valid format in every cell, then you will need to add validation logic to this event. For example, use int.TryParse()
and check for null
values, and validate the length of the string
splits.
这篇关于如何在DataGridView中将字符串作为数字排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!