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?
source share