Link to home
Start Free TrialLog in
Avatar of nielsentech
nielsentech

asked on

WinForms, sort DataGridView columns when header is clicked

Microsoft has a good sample of how to pull of datagrid sorting here: http://msdn.microsoft.com/en-us/library/0868ft3z.aspx.

However, their samples uses the click of a button. I want the user to be able to simply click the column header and have the column sort. There are two problems:

1) The process that adds the selected column to DataGridView.SelectedColumns must run asynchronously after the ColumnHeaderClick event. Often times, it is null, causing my code to fail. If I step through the code, allowing the asynchronous call to finish up (I'm assuming), then things work great. I was thinking I could use Thread.Sleep(), but that seems kind of sloppy. There must be some better way...

2) Even with MultiSelect set to false, after sorting a column, the next column I click gets selected. Very Strange.

The code is attached. It is almost identical to the microsoft example. MultiSelect is false, SelectionMode is ColumnHeaderSelect, and each column's sort mode is Programmatic. The datagrid is bound to a datatable.

Attached is an image of the strange selection behavior.


private void dgOrders_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {            
 
            // Check which column is selected, otherwise set NewColumn to null.
            DataGridViewColumn newColumn =                              
                dgOrders.SelectedColumns.Count > 0 ?
                dgOrders.SelectedColumns[0] : null;
 
            DataGridViewColumn oldColumn = dgOrders.SortedColumn;
            ListSortDirection direction;
 
            // If oldColumn is null, then the DataGridView is not currently sorted.
            if (oldColumn != null)
            {
                // Sort the same column again, reversing the SortOrder.
                if (oldColumn == newColumn &&
                    dgOrders.SortOrder == SortOrder.Ascending)
                {
                    direction = ListSortDirection.Descending;
                }
                else
                {
                    // Sort a new column and remove the old SortGlyph.
                    direction = ListSortDirection.Ascending;
                    oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
                }
            }
            else
            {
                direction = ListSortDirection.Ascending;
            }
 
            dgOrders.Sort(newColumn, direction);
            
            newColumn.HeaderCell.SortGlyphDirection =
                direction == ListSortDirection.Ascending ?
                SortOrder.Ascending : SortOrder.Descending;
        }

Open in new window

experts-selection.jpg
Avatar of nielsentech
nielsentech

ASKER

Just an update: I opted to use a dropdown to select the column to sort. This works fine and I don't have any of the above problems that way, but I would still be curious to know if anybody knows a solution for this...

ASKER CERTIFIED SOLUTION
Avatar of graye
graye
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow... I feel silly having even asked :) I'm not sure how I got going down that path. At least I didn't spend long on it. Thanks.