Winform DatagridView Numeric Column Sorting

前端 未结 6 1878
有刺的猬
有刺的猬 2021-02-06 05:26

I am using just a simple DataGridView to hold a bunch of data (Funny that).

I have decimals in a particular column. But when it comes to ordering by that decimal column

6条回答
  •  清酒与你
    2021-02-06 06:17

    Numeric types have a built in CompareTo function, which can be used as the SortResult of the SortCompare event.

        private void dataGridView_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            if (e.Column.Index == 0)
            {
                e.SortResult = int.Parse(e.CellValue1.ToString()).CompareTo(int.Parse(e.CellValue2.ToString()));
                e.Handled = true;
            }
        }
    

    This is of course assuming you know the type that was put into the DataGridView to begin with.

提交回复
热议问题