Sort in a DataGridViewTextBoxColumn

This question is closely related to these two ( this and this ), but I do not think they give a satisfactory 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 heading, each should be sorted according to their type: string in alphabetical order and numerical values ​​numerically. I just 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 , numeric values ​​are also sorted alphabetically, for example:

 1, 11, 2, 3, 42, 5 

Apparently, as an easy way to get around this, according to some threads (like here and here ), you should immediately solve the following problem:

 doubleColumn.ValueType = typeof(double); intColumn.ValueType = typeof(int); 

However, this solution just doesn't work in my project: the values ​​are still sorted alphabetically. So the question is: why not? In Debugger, I could see that the type of the value actually changed to (in the case of double ) System.Double , so something is happening at least. But how can I make sure that he really sorts it accordingly without writing his own sorter?

+6
source share
4 answers

You can handle the SortCompare event to change the sorting method, for example:

 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 assumes that your cell values ​​are converted to int .

You said that your DataGridView does not have a DataSource , which means you Add rows manually, so I think you should use numeric values ​​instead of string for your cells. This will do the sorting job as you wish.

+11
source

If you are using a DataTable , then you must set the DataType to a DataColumn . Setting a ValueType to a DataGridViewTextBoxColumn will not help.

You can install it when you create it:

 table.Columns.Add("Number", typeof(int)); 
+1
source

Changing a column from string to int32 might be useful:

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

It works for me. Hope this helps.

0
source

Setting the ValueType column to typeof (int) will work, just remember to make sure that integers are entered in this column. If the rest of your data contains strings, it can be easy to forget to convert your number from string to int.

0
source

All Articles