how to sort string as number in datagridview in winforms

前端 未结 4 1158
我在风中等你
我在风中等你 2020-12-01 14:33

I have string column with numbers in a datagridview.It is not bound, I would like to sort it number wise I used

colid.ValueType = typeof(int);
grid.Sort(co         


        
4条回答
  •  悲&欢浪女
    2020-12-01 14:58

    Create a class like:

    class Sort : IComparer
    {
        public int Compare(object x, object y)
        {
            return -int.Parse((string)x).CompareTo(int.Parse((string)y)); //sort descending
            //return int.Parse((string)x).CompareTo(int.Parse((string)y)); //sort ascending
        }
    }
    

    and do

    grid.Sort( new Sort() );
    

提交回复
热议问题