Winform DatagridView Numeric Column Sort

I use a simple DataGridView to store a bunch of data (Funny that).

I have decimal numbers in a specific column. But when it comes to ordering this decimal column, he orders it incorrectly. For example:

The start order can be:

  • 0.56
  • 3.45
  • 500.89
  • 20078.90
  • 1,56
  • 100.29
  • 2,39

The final order will be:

  • 0.56
  • 100.29
  • 1,56
  • 20078.90
  • 2,39
  • 3.45
  • 500.89

As you can see, he orders it starting from the first number. And then orders it that way.

I thought, maybe I could set the column to another "ColumnType", and that could automatically do that. But there are no column types Numeric or Decimal.

I was on MSDN considering the problem, and I could find a sorting method that I can use in the DataGridView. But the explanation was a little over my head, and the examples did not use numbers, only text, so I could not see how I had to switch things.

Any help would be greatly appreciated.

+7
c # winforms datagridview
source share
6 answers

This can be solved by adding a SortCompare event handler to 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, this is a 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 before the first cell is sorted.

Note that on my test field, the only numeric column was the first (with index 0), so I checked the column index.

In addition, depending on your needs and data, you can clarify my code - for example, my code will throw an exception if for some reason you have non-numeric data in your column.

You may have seen this, but here is a link to the MSDN page for configuring DataGridView sorting. As you say, they only deal with text.

+7
source share

I had the same problem. I tried using an event handler, as David Hall mentioned. I used the ValueType property when defining a DataGridView. Now it sorts as doubles, no event handler code is required

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

You can also format the column using

 dataGridView2.Columns[int index].DefaultCellStyle.Format = string format; 
+6
source share

Numeric types have a built-in CompareTo function that can be used as the SortResult of a 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, of course, assumes that you know the type that was placed in the DataGridView to begin with.

+1
source share

The database column type should be int or double or float, not varchar or something else. Therefore, you need to change your value type in the database ... You do not need to write any code or something that it directly sorts when you click on the column heading ...

+1
source share

This is sorting by character. You need to make a column type float so that it knows which comparison operator to apply.

(That is, you need to make the column type in the data set float, I believe this will work.)

0
source share

Your problem is sorting the datagridview by row. Try string to float when copying this cell to datagrid .

0
source share

All Articles