Advertisement

05.22.2008 at 08:33AM PDT, ID: 23424796
[x]
Attachment Details

WinForms, sort DataGridView columns when header is clicked

Asked by nielsentech in C# Programming Language, Visual Studio .NET 2005

Tags: C#

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.

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
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;
        }
Attachments:
 
Snapshot of strange selection behavior
Snapshot of strange selection behavior
 
[+][-]05.22.2008 at 01:19PM PDT, ID: 21627203

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.26.2008 at 02:25PM PDT, ID: 21648167

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C# Programming Language, Visual Studio .NET 2005
Tags: C#
Sign Up Now!
Solution Provided By: graye
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628