Winform DatagridView Numeric Column Sorting

前端 未结 6 1880
有刺的猬
有刺的猬 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 05:54

    Your problem is the datagridview is sorting by string. Try casting the string to float when you copy that cell into the datagrid.

    0 讨论(0)
  • 2021-02-06 06:07

    Your database column type should be int or double or float, not varchar or something.... So you have to change your value-type in the database... You no need to write any code or something it directly sort when you click on the column Header...

    0 讨论(0)
  • 2021-02-06 06:16

    I had the same problem. I tried using the event handler as David Hall mentioned. I used the ValueType property when defining the DataGridView. It now sorts as doubles, no custom event handler code needed

    dataGridView1.Columns[int index].ValueType = typeof(double);
    

    You can also format the column using

    dataGridView2.Columns[int index].DefaultCellStyle.Format = string format;
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 06:20

    You can solve this by adding a handler for the SortCompare event on the DataGridView with the following code:

    private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
    {
        if (e.Column.Index == 0)
        {
            if (double.Parse(e.CellValue1.ToString()) > double.Parse(e.CellValue2.ToString()))
            {
                e.SortResult = 1;
            }
            else if (double.Parse(e.CellValue1.ToString()) < double.Parse(e.CellValue2.ToString()))
            {
                e.SortResult = -1;
            }             
            else
            {
                e.SortResult = 0;
            }
            e.Handled = true;
       }
    }
    

    From MSDN there is this description of the SortResult values:

    Less than zero if the first cell will be sorted before the second cell; zero if the first cell and second cell have equivalent values; greater than zero if the second cell will be sorted before the first cell.

    Note that in my test bed the only numeric column was the first (with index 0) so that is why I have the check on the column index.

    Also, depending on your needs and data you may want to refine my code - for example, my code will throw an exception if for some reason you have non numeric data in your column.

    You have probably seen it, but here is a link to the MSDN page on customising the DataGridView sorting. As you say, they only deal with text.

    0 讨论(0)
  • 2021-02-06 06:21

    It is sorting it by character. You need to make the column type float so it knows what comparison operator to apply.

    (That is you need to make the column type in your dataset float, I believe that will work.)

    0 讨论(0)
提交回复
热议问题