2

I have two questions:

  1. Is there a way to sort the DataGridView by column - I want to do this using code so that one of my methods can sort the DataGridView. (Same effect as if the user clicks on the column header, but without the clicking.)

  2. When I sort a column with decimal values (of type double) the sorting believes that 9.99 is larger than 24.99 - is there any way to prevent this type of sorting behaviour?

Update: Question number 1 was solved thanks to a link in the comments. I also found the cause of the issue to question number 2: The values in my column are (double)price + "€". I tried removing the + "€" and it started sorting the way I want. Now I want it to be able to sort, while keeping the € somehow.

4
  • For the sorting, the below question may help stackoverflow.com/questions/16956251/… Commented Mar 13, 2017 at 16:41
  • Can you show us the code/xaml for that column to see how the data is going in/being formatted? Commented Mar 13, 2017 at 16:42
  • 1
    First question is a duplicate of stackoverflow.com/questions/806725/… Commented Mar 13, 2017 at 16:44
  • 1
    You need to show what you've tried with sorting. As @Josh said, how you're putting the data into the DataGridView. Remove the first question as each question should only be a single question here. Commented Mar 13, 2017 at 17:40

1 Answer 1

0

Since the first question was correctly covered by stuartd, we'll focus on the second:

When I sort a column with decimal values (of type double) the sorting believes that 9.99 is larger than 24.99 - is there any way to prevent this type of sorting behaviour?

As you've noticed, removing + "€" corrected your issue because with it that column is performing a string comparison instead of the intended double comparison. You can fix this by using a CultureInfo to format the column as a currency column instead.

CultureInfo ci = new CultureInfo("fr-FR");
this.dataGridView1.Columns[0].DefaultCellStyle.FormatProvider = ci.NumberFormat;
this.dataGridView1.Columns[0].DefaultCellStyle.Format = "c";

unsorted list sort asc sort desc

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.