c# - Sort DataGridView cells in programmatic way -


i have datagridview called dgv, , i'm trying use sort function sort first cell in programmatic way using :

dgv.sort(rang, system.componentmodel.listsortdirection.descending); 

but on output have :

1, 10, 100, 11, 12, ..., 19, 2, 20, 21, ...

i have set sortmode of rang cell programmatic, have output.

what's problem ? !

thanks.

appears sorting based on place value, how string datatype sorted. need make sure column sort on int.

edit: if going attempt parse string value, recommend using tryparse( ) method. here example of comparer might 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 );     } 

Comments