Link to home
Create AccountLog in
.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

Avatar of nielsentech
nielsentech

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

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of nielsentechnielsentech

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 grayegraye🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

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.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

.NET Programming

.NET Programming

--

Questions

--

Followers

Top Experts

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.