none
Maintain and set the SortColumn and SortDirection of DatagridView after refresh RRS feed

  • Question

  • How to maintain and set the SortColumn and SortDirection when I am refreshing the grid.

    I am trying like this..
      int _sortColumn;
      SortOrder _sortDirection = SortOrder.None;
                   
     if (dgv_users.Rows.Count > 0)
                    {
                          //get the sort column and sort direction
                        _sortColumn = dgv_users.SortedColumn.Index;
                        _sortDirection = dgv_users.SortOrder;                   
                    }

    Now after refreshing the grid,
    How I can set these variables to the grid?
    I ma going rite? and if Yes..what to do next?
    Thursday, October 29, 2009 6:45 AM

Answers

  • Hi,

    Normally it should keep sorting after new datasource is bound.

    Could you try this.

            int _sortColumn=-1;
            SortOrder _sortDirection = SortOrder.None;       
            public void Refresh()
            {
                if (dgv.SortedColumn == null)
                {
                    _sortColumn = -1;
                }
                else
                {
                    _sortColumn = dgv.SortedColumn.Index;
                    _sortDirection = dgv.SortOrder;
                }
    
                dgv.DataSource = dt;
                if (_sortColumn != -1)
                {
                    switch (_sortDirection)
                    {
                        case SortOrder.Ascending:
                            dgv.Sort(dgv.Columns[_sortColumn], ListSortDirection.Ascending);
                            break;
                        case SortOrder.Descending:
                            dgv.Sort(dgv.Columns[_sortColumn], ListSortDirection.Descending);
                            break;
                        case SortOrder.None:
                            break;
                    }
                }
                
            } 
    • Marked as answer by Jing0Moderator Tuesday, November 3, 2009 9:42 AM
    Monday, November 2, 2009 6:41 PM
  • Hi,

     

    Use DataGridView.Sort() method.

    dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);

     

    If you need custom sorting, use SortCompare event.

    How to: Customize Sorting in the Windows Forms DataGridView Control

    http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

    A FAQ about DataGridView sorting: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e/#faq20

     

    Best regards,

    Ling Wang

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Tuesday, November 3, 2009 9:42 AM
    Monday, November 2, 2009 8:26 AM
    Moderator

  • Hey Thanks a lot Tamer Oz :)
                         Your code really helped me to work it out. And Finally I done it !


    Here just adding for reference..so it could be helpful to others also.

    I set a default column index for _sortColumn.
      int _sortColumn = 3;
      SortOrder _sortDirection = SortOrder.Ascending; 
    private void FillUsers()
    {
          dgv_users.DataSource = bindingUser;
    
           if (_sortColumn != -1)
           {
               switch (_sortDirection)
                         {
                             case SortOrder.Ascending:
                                 dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Ascending);
                                 break;
                             case SortOrder.Descending:
                                 dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Descending);
                                 break;
                             case SortOrder.None:
                                 break;
                         }
                     }  
    }        

    By using this when my DatagridView will be loaded it sort the default column 3.

    Even I wanted to change the SortColumns and Directions So..I added

    private void dgv_users_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (dgv_users.Columns[e.ColumnIndex].SortMode != DataGridViewColumnSortMode.NotSortable)
                {
                    if (e.ColumnIndex == _sortColumn)
                    {
                        if (_sortDirection == SortOrder.Ascending)
                            _sortDirection = SortOrder.Descending;
                        else
                            _sortDirection = SortOrder.Ascending;
                    }
                    _sortColumn = e.ColumnIndex;
    
                    switch (_sortDirection)
                    {
                        case SortOrder.Ascending:
                            dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Ascending);
                            break;
                        case SortOrder.Descending:
                            dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Descending);
                            break;
                        case SortOrder.None:
                            break;
                    }
                }
                 
            }
    and It's Done :)



    • Marked as answer by TejaswiniJ Tuesday, November 3, 2009 9:51 AM
    Tuesday, November 3, 2009 9:50 AM

All replies

  • You can store this values in a class level variable and use again when you rebind the data.
    Thursday, October 29, 2009 8:27 AM
  • Hi,
     I think  I am, on wrong track.. :( Can U tell me how I can access SortColumn and SortDirection and set it again ?
     I m binding a DataTable to DataSource of DataGridView control. I am not using DataView control.
    It's urgent !

    Thanks in advance
    Monday, November 2, 2009 4:52 AM
  • Hi,

     

    Use DataGridView.Sort() method.

    dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Descending);

     

    If you need custom sorting, use SortCompare event.

    How to: Customize Sorting in the Windows Forms DataGridView Control

    http://msdn.microsoft.com/en-us/library/0868ft3z.aspx

    A FAQ about DataGridView sorting: http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e/#faq20

     

    Best regards,

    Ling Wang

     


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Jing0Moderator Tuesday, November 3, 2009 9:42 AM
    Monday, November 2, 2009 8:26 AM
    Moderator
  • Hi,

    Normally it should keep sorting after new datasource is bound.

    Could you try this.

            int _sortColumn=-1;
            SortOrder _sortDirection = SortOrder.None;       
            public void Refresh()
            {
                if (dgv.SortedColumn == null)
                {
                    _sortColumn = -1;
                }
                else
                {
                    _sortColumn = dgv.SortedColumn.Index;
                    _sortDirection = dgv.SortOrder;
                }
    
                dgv.DataSource = dt;
                if (_sortColumn != -1)
                {
                    switch (_sortDirection)
                    {
                        case SortOrder.Ascending:
                            dgv.Sort(dgv.Columns[_sortColumn], ListSortDirection.Ascending);
                            break;
                        case SortOrder.Descending:
                            dgv.Sort(dgv.Columns[_sortColumn], ListSortDirection.Descending);
                            break;
                        case SortOrder.None:
                            break;
                    }
                }
                
            } 
    • Marked as answer by Jing0Moderator Tuesday, November 3, 2009 9:42 AM
    Monday, November 2, 2009 6:41 PM

  • Hey Thanks a lot Tamer Oz :)
                         Your code really helped me to work it out. And Finally I done it !


    Here just adding for reference..so it could be helpful to others also.

    I set a default column index for _sortColumn.
      int _sortColumn = 3;
      SortOrder _sortDirection = SortOrder.Ascending; 
    private void FillUsers()
    {
          dgv_users.DataSource = bindingUser;
    
           if (_sortColumn != -1)
           {
               switch (_sortDirection)
                         {
                             case SortOrder.Ascending:
                                 dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Ascending);
                                 break;
                             case SortOrder.Descending:
                                 dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Descending);
                                 break;
                             case SortOrder.None:
                                 break;
                         }
                     }  
    }        

    By using this when my DatagridView will be loaded it sort the default column 3.

    Even I wanted to change the SortColumns and Directions So..I added

    private void dgv_users_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (dgv_users.Columns[e.ColumnIndex].SortMode != DataGridViewColumnSortMode.NotSortable)
                {
                    if (e.ColumnIndex == _sortColumn)
                    {
                        if (_sortDirection == SortOrder.Ascending)
                            _sortDirection = SortOrder.Descending;
                        else
                            _sortDirection = SortOrder.Ascending;
                    }
                    _sortColumn = e.ColumnIndex;
    
                    switch (_sortDirection)
                    {
                        case SortOrder.Ascending:
                            dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Ascending);
                            break;
                        case SortOrder.Descending:
                            dgv_users.Sort(dgv_users.Columns[_sortColumn], ListSortDirection.Descending);
                            break;
                        case SortOrder.None:
                            break;
                    }
                }
                 
            }
    and It's Done :)



    • Marked as answer by TejaswiniJ Tuesday, November 3, 2009 9:51 AM
    Tuesday, November 3, 2009 9:50 AM