开发者

Sort DataGridView cells in programmatic way

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.D开发者_运维知识库escending);

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜