I was looking for an example of sorting a DataGridView on multiple columns, but I can't seem to find an example that does what I would like.
Basically, I have a related DataGridView control (bound to a DataTable / DataView), and the associated DataTable has two columns: - priority and date. I would like to sort by date in priority order. That is, the priority column takes a preliminary estimate, then its date, but both can be ascending or descending.
So, for example, I can have a low priority, an early date first (asc priority order, ascending date) and, clicking on the heading of a date column, switch to low priority, late date first (asc priority order, desc date). If I then click on priority, first I want to have a high priority, and then a late date (the current sort order for the date column is the desc, date desc priority order), but then you can click the heading of the date column to switch to high priority, to early date (desc. order, ascending date).
Ideally, I would like the glyphs on both columns to be displayed in both ascending and descending order.
Any ideas or pointers would be greatly appreciated.
This (see below) seems pretty close, but the glyphs are not working correctly.
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"; } } }