Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3696346
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:43:41+00:00 2026-05-19T04:43:41+00:00

I’ve searched for an example of sorting a DataGridView on multiple columns, but don’t

  • 0

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.

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.

So, for example, I may have low priority, early date first (order by priority asc, date asc), and, by clicking the date column header, switch to low priority, late date first (order by priority asc, date desc). 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 – order by priority desc, date desc), but then be able to click the date column header to switch to high priority, early date (order by priority desc, date asc).

Ideally, I would like sort glyphs on both columns to show ascending or descending.

Any ideas or pointers would be gratefully received.

This (see below) seems to get pretty close, but the glyphs aren’t working right.

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";
     }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:43:42+00:00Added an answer on May 19, 2026 at 4:43 am

    Ok.

    Following from Cody’s suggestions above, I now have something which seems to work as expected. I have sub-classed the HeaderCell and over-ridden the Paint method (but cheated by setting the SortGlyphDirection immediately prior to the base.Paint) and the DGV now paints multiple sorting glyphs.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication4
    {
      public partial class Form1 : Form
      {
         DataSet1 dataset;
    
         public Form1()
         {
            InitializeComponent();
    
            dataset = new DataSet1(); // three columns: Priority(Int32), Date (DateTime) and Description(String)
            dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("01-jan-10"), "this");
            dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("02-jan-10"), "is");
            dataset.DataTable1.AddDataTable1Row(1, DateTime.Parse("03-jan-10"), "a");
            dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("04-jan-10"), "sample");
            dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("05-jan-10"), "of");
            dataset.DataTable1.AddDataTable1Row(2, DateTime.Parse("06-jan-10"), "the");
            dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("07-jan-10"), "data");
            dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("08-jan-10"), "in");
            dataset.DataTable1.AddDataTable1Row(3, DateTime.Parse("09-jan-10"), "use");
    
            dataGridView1.DataSource = dataset.DataTable1.DefaultView;
    
            dataGridView1.AllowUserToAddRows = false;
    
            dataGridView1.Columns[0].HeaderCell = new MyDataGridViewColumnHeaderCell();
            dataGridView1.Columns[1].HeaderCell = new MyDataGridViewColumnHeaderCell();
    
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.Programmatic;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.Programmatic;
         }
    
         private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
         {
            DataGridViewColumn clickedColumn = dataGridView1.Columns[e.ColumnIndex];
    
            if (clickedColumn.HeaderCell is MyDataGridViewColumnHeaderCell)
            {
               DoMultiColumnSort();
            }
            else
            {
               dataGridView1.Columns.OfType<DataGridViewColumn>()
                                    .Where(column => column.HeaderCell is MyDataGridViewColumnHeaderCell)
                                    .ForEach(column => ((MyDataGridViewColumnHeaderCell)column.HeaderCell).SortOrderDirection = SortOrder.None);
            }
    
            this.textBox1.Text = dataset.DataTable1.DefaultView.Sort;
         }
    
         private void DoMultiColumnSort()
         {
            var sortClauses = dataGridView1.Columns.OfType<DataGridViewColumn>()
                                                   .Where(column => column.HeaderCell is MyDataGridViewColumnHeaderCell)
                                                   .Select(column => GetSortClause(column));
    
            dataset.DataTable1.DefaultView.Sort = String.Join(",", sortClauses);
         }
    
         private String GetSortClause(DataGridViewColumn column)
         {
            SortOrder direction = column.HeaderCell.SortGlyphDirection;
    
            if (column.HeaderCell is MyDataGridViewColumnHeaderCell)
            {
               direction = ((MyDataGridViewColumnHeaderCell)column.HeaderCell).SortOrderDirection;
            }
    
            return column.DataPropertyName + " " + (direction == SortOrder.Descending ? "DESC" : "ASC");
         }
      }
    
      public partial class MyDataGridViewColumnHeaderCell : DataGridViewColumnHeaderCell
      {
         public SortOrder SortOrderDirection { get; set; } // defaults to zero = SortOrder.None;
    
         protected override void Paint(System.Drawing.Graphics graphics, System.Drawing.Rectangle clipBounds, System.Drawing.Rectangle cellBounds, int rowIndex, DataGridViewElementStates dataGridViewElementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
         {
            this.SortGlyphDirection = this.SortOrderDirection;
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);
         }
    
         public override object Clone()
         {
            MyDataGridViewColumnHeaderCell result = (MyDataGridViewColumnHeaderCell)base.Clone();
            result.SortOrderDirection = this.SortOrderDirection;
            return result;
         }
    
         protected override void OnClick(DataGridViewCellEventArgs e)
         {
            this.SortOrderDirection = (this.SortOrderDirection != SortOrder.Ascending) ? SortOrder.Ascending : SortOrder.Descending;
            base.OnClick(e);
         }
      }
    
      public static partial class Extensions
      {
         public static void ForEach<T>(this IEnumerable<T> value, Action<T> action) { foreach (T item in value) { action(item); } }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.