DataGridView列排序

调用

DataGridView1.BindSortCompare()

 

公共类

public static void BindSortCompare(this DataGridView dg)
        {
            dg.SortCompare += (s, e) =>
            {
                double value1, value2;

                if (!double.TryParse(e.CellValue1?.ToString(), out value1))
                    return;
                if (!double.TryParse(e.CellValue2?.ToString(), out value2))
                    return;

                if (value1 == value2)
                    e.SortResult = 0;
                else if (value1 < value2)
                    e.SortResult = -1;
                else
                    e.SortResult = 1;

                e.Handled = true;
            };
        }

 

posted @ 2022-04-23 14:24  普天2022  阅读(152)  评论(0编辑  收藏  举报