I have a datagridview called DGV, and I'm trying to use the sort function to sort the first cell in a programmatic way using :
DGV.Sort(Rang, System.ComponentModel.ListSortDirection.Descending);
but on output I have this :
1, 10, 100, 11, 12, ..., 19, 2, 20, 21, ...
I have already set the SortMode of Rang cell to Programmatic, but I have always this output.
What's the problem ? !
Thanks.
Appears to be sorting based on place value, which is how a string
datatype would be sorted. You need to make sure that the column you sort on is an int
.
Edit:
If you are going to attempt to parse your string value, I recommend using the TryParse( )
method. Here is an example of what your Comparer might look like:
int Compare ( object obj1, object obj2 )
{
IComparer _comparer = Comparer.Default;
int val1, val2;
if (!Int32.TryParse ( obj1.ToString ( ), out val1 ))
return -1;
if (!Int32.TryParse ( obj2.ToString ( ), out val2 ))
return -1;
return _comparer.Compare ( val1, val2 );
}