none
Specific custom sorting for a DataView (C#) RRS feed

  • Question

  • Can anyone please provide the necessary code or explanation of how to sort a DataView column in C# using the conventional American grading scale system...

     

    A+

    A

    A-

    B+

    B

    B-

    ....

     

     

    I have looked up many other examples, but am struggling to make connections. Thank you so much!

    • Moved by VMazurModerator Monday, July 12, 2010 10:43 AM (From:ADO.NET Managed Providers)
    Sunday, July 11, 2010 3:53 AM

Answers

  • Hi Eric,

    The standard System.Data.DataView implements IBindingListView and IBindingList and it delegates sorting to the underlying IBindingList, which cannot do custom sorting.

    You could choose to write your own CustomDataView class and implement IComparer in your own way.

    HOW TO: Implement a Custom DataView Class in Visual Basic .NET
    http://support.microsoft.com/kb/325682/en-us

    But most times, you will want to use a List<T> instead of a DataView, and call List<T>.Sort(Comparison<T>) to get the data sorted in the way you want it. 

    (I still don't know much about your concrete scenario, so I'll leave it to you to figure out what fits you best.)

     

    Marcel

     

    Monday, July 12, 2010 10:57 AM

All replies

  • Hi Eric,

    I suppose you ment DataGridView?

    If so, you need to use customized programmatic sorting. Here is a simple example for an unbound DataGridView:

    using System;
    using System.Linq;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace CustomSortDgvCol
    {
     public partial class Form1 : Form
     {
      public Form1()
      {
       InitializeComponent();
      }
    
      private void Form1_Load(object sender, EventArgs e)
      {
       string[] names = "Johnson Anderson Peer Smith O'Hara Buwaro Mingus Roberts Nuntius Derby".Split(' ');
       string[] gradings = "F A B+ A- B C- B- C+ C A+".Split(' ');
    
       dataGridView1.ColumnCount = 2;
       dataGridView1.Columns[0].Name = "Grading";
       dataGridView1.Columns[1].Name = "Last";
       dataGridView1.Columns["Grading"].HeaderText = "Grading";
       dataGridView1.Columns["Last"].HeaderText = "Last Name";
       dataGridView1.Columns["Grading"].SortMode =
        DataGridViewColumnSortMode.Programmatic;
       dataGridView1.Columns["Last"].SortMode =
        DataGridViewColumnSortMode.Programmatic;
    
       for (int i = 0; i < gradings.Length; i++)
       {
        dataGridView1.Rows.Add(new string[] {gradings[i], names[i]});
       }
      }
    
      private void buttonSort_Click(object sender, EventArgs e)
      {
       AmericanGradingScaleSortHelper sortHelper = new AmericanGradingScaleSortHelper();
       dataGridView1.Sort(sortHelper);
      }
     }
    
     public class AmericanGradingScaleSortHelper : IComparer
     {
      string[] gradingScale = "A+ A A- B+ B B- C+ C C- F".Split(' ');
    
      public int Compare(object x, object y)
      {
       DataGridViewRow dataGridViewRow1 = (DataGridViewRow)x;
       DataGridViewRow dataGridViewRow2 = (DataGridViewRow)y;
    
       string gradingX = (string)dataGridViewRow1.Cells["Grading"].FormattedValue;
       string gradingY = (string)dataGridViewRow2.Cells["Grading"].FormattedValue;
    
    
       if (gradingScale.Contains(gradingX) && gradingScale.Contains(gradingY))
       {
        int intPosX = Array.FindIndex<string>(gradingScale, g => g == gradingX);
        int intPosY = Array.FindIndex<string>(gradingScale, g => g == gradingY);
    
        if (intPosX == intPosY)
         return 0;
        else if (intPosX > intPosY)
         return 1;
        else
         return -1;
       }
       else
       {
        throw new ArgumentException("Unknown grading");
       }
      }
     }
    }
    
    

     

    You can find other ways of sorting the DataGridView here:

    How to: Customize Sorting in the Windows Forms DataGridView Control
    http://msdn.microsoft.com/en-us/library/ms171608.aspx

    Column Sort Modes in the Windows Forms DataGridView Control
    http://msdn.microsoft.com/en-us/library/95scxcdy.aspx

    To customize sorting for columns bound to an external data source, you must use the sorting operations provided by the data source. To get to there you could change the type of the first columns from typeof(string) to a new type, say Grading, and let the new type implement IComparable and IComparer:

    How to use the IComparable and IComparer interfaces in Visual C#
    http://support.microsoft.com/kb/320727/en-us

     

    Marcel

    Sunday, July 11, 2010 9:05 AM
  • I really appreciate this, as it is very easy to understand. However, I did mean DataView . I should have been more specific. I am programming in C# but am writing a WPF desktop application. DataView does not support SortMode. I have fiddled around with your method for the DataView but it has not been successful. Is there some way to achieve the same goal in a DataView?
    Sunday, July 11, 2010 2:29 PM
  • Hi Eric,

    The standard System.Data.DataView implements IBindingListView and IBindingList and it delegates sorting to the underlying IBindingList, which cannot do custom sorting.

    You could choose to write your own CustomDataView class and implement IComparer in your own way.

    HOW TO: Implement a Custom DataView Class in Visual Basic .NET
    http://support.microsoft.com/kb/325682/en-us

    But most times, you will want to use a List<T> instead of a DataView, and call List<T>.Sort(Comparison<T>) to get the data sorted in the way you want it. 

    (I still don't know much about your concrete scenario, so I'll leave it to you to figure out what fits you best.)

     

    Marcel

     

    Monday, July 12, 2010 10:57 AM