title: Sort a DataGridView on multiple columns?
body: <p>I've searched for an example of sorting a DataGridView on multiple columns, but don't seem to be able to find an example which does what I would like.</p>
  
  <p>Basically, I have a bound DataGridView control (bound to a DataTable/DataView), and the bound DataTable has two columns:- priority and date. I would like to sort by date within priority. That is, the priority column takes precendence, then its the date but both can be ascending or descending.</p>
  
  <p>So, for example, I may have low priority, early date first <em>(order by priority asc, date asc)</em>, and, by clicking the date column header, switch to low priority, late date first <em>(order by priority asc, date desc)</em>. If I then click on the priority, I would like to have high priority first, then late date (the current sort order for the date column - <em>order by priority desc, date desc)</em>, but then be able to click the date column header to switch to high priority, early date <em>(order by priority desc, date asc)</em>.</p>
  
  <p>Ideally, I would like sort glyphs on both columns to show ascending or descending.</p>
  
  <p>Any ideas or pointers would be gratefully received.</p>
  
  <p>This (see below) seems to get pretty close, but the glyphs aren't working right.</p>
  
  <pre><code>using System;
  using System.Windows.Forms;
  
  namespace WindowsFormsApplication4
  {
    public partial class Form1 : Form
    {
       DataSet1 dataset;
  
       public Form1()
       {
          InitializeComponent();
  
          dataset = new DataSet1(); // two columns: Priority(Int32) and date (DateTime)
          dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("01-jan-10"));
          dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("02-jan-10"));
          dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("03-jan-10"));
          dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("04-jan-10"));
          dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("05-jan-10"));
          dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("06-jan-10"));
          dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("07-jan-10"));
          dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("08-jan-10"));
          dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("09-jan-10"));
  
          dataGridView1.DataSource = dataset.DataTable1.DefaultView;
  
          dataGridView1.AllowUserToAddRows = false;
  
          dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
          dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.Programmatic;
  
          dataGridView1.Columns[0].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
          dataGridView1.Columns[1].HeaderCell.SortGlyphDirection = SortOrder.Ascending;
       }
  
       private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
          DataGridViewColumn[] column = new[] { dataGridView1.Columns[0], dataGridView1.Columns[1] };
  
          DataGridViewColumnHeaderCell headerCell = dataGridView1.Columns[e.ColumnIndex].HeaderCell;
  
          if (headerCell.SortGlyphDirection != SortOrder.Ascending)
             headerCell.SortGlyphDirection = SortOrder.Ascending;
          else
             headerCell.SortGlyphDirection = SortOrder.Descending;
  
          String sort = column[0].DataPropertyName + " " + fnSortDirection(column[0])
                      + ", "
                      + column[1].DataPropertyName + " " + fnSortDirection(column[1]);
          dataset.DataTable1.DefaultView.Sort = sort;
          this.textBox1.Text = sort;
       }
  
       private String fnSortDirection(DataGridViewColumn column)
       {
          return column.HeaderCell.SortGlyphDirection != SortOrder.Descending ? "asc" : "desc";
       }
    }
  }
  </code></pre>