Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

c# - Sorting numerically in a DataGridViewTextBoxColumn

This question is closely related to these two (this and this) but I don't think they give a satisfying answer.

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});

However, since the default representation is string, the numerical values also get sorted alphabetically, for example like this:

1, 11, 2, 3, 42, 5

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);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
     }
}

NOTE: The above code supposes your cell values are convertible to int.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...