DataGridView save filtering after reload

前端 未结 4 2120
春和景丽
春和景丽 2021-02-10 20:34

I have some problem with DataGridView in C#.

case is:

I do some update on database then I reload DataGridView with new values:

myDataGridView.Dat         


        
相关标签:
4条回答
  • 2021-02-10 21:10

    Ok, I found the solution, maybe it will help somebody: [btw. I made some language mistake filtering = sorting ;-)]

       DataGridViewColumn oldColumn = dataGridView1.SortedColumn;
    
       ListSortDirection direction;
       if (dataGridView1.SortOrder == SortOrder.Ascending) direction = ListSortDirection.Ascending;
       else direction = ListSortDirection.Descending;
    
       databaseUpdateFunction();
    
       DataGridViewColumn newColumn = dataGridView1.Columns[oldColumn.Name.ToString()];
       dataGridView1.Sort(newColumn,direction);
       newColumn.HeaderCell.SortGlyphDirection =
                        direction == ListSortDirection.Ascending ?
                        SortOrder.Ascending : SortOrder.Descending;
    

    I used parts of code from : link text

    0 讨论(0)
  • 2021-02-10 21:10

    This worked for me:

    
    
        //preserve existing sort column and direction
        ListSortDirection direction;
        DataGridViewColumn oldsort = MyDataGridView.SortedColumn;
        if (MyDataGridView.SortOrder == SortOrder.Ascending)
        {   direction = ListSortDirection.Ascending; } else { direction = ListSortDirection.Descending; }
    
        //this is refresh
        MyDataGridView.DataSource = data;
    
        //reapply sort and direction
        if (oldsort != null) { MyDataGridView.Sort(MyDataGridView.Columns[oldsort.Name], direction); }
    
    
    0 讨论(0)
  • 2021-02-10 21:15

    I took kuba's solution, and put it in a utility class I can use on any DataGridView:

        private static ListSortDirection _oldSortOrder;
        private static DataGridViewColumn _oldSortCol;
    
        /// <summary>
        /// Saves information about sorting column, to be restored later by calling RestoreSorting
        /// on the same DataGridView
        /// </summary>
        /// <param name="grid"></param>
        public static void SaveSorting(DataGridView grid)
        {
            _oldSortOrder = grid.SortOrder == SortOrder.Ascending ?
                ListSortDirection.Ascending : ListSortDirection.Descending;
            _oldSortCol = grid.SortedColumn;
        }
    
        /// <summary>
        /// Restores column sorting to a datagrid. You MUST call this AFTER calling 
        /// SaveSorting on the same DataGridView
        /// </summary>
        /// <param name="grid"></param>
        public static void RestoreSorting(DataGridView grid)
        {
            if (_oldSortCol != null)
            {
                DataGridViewColumn newCol = grid.Columns[_oldSortCol.Name];
                grid.Sort(newCol, _oldSortOrder);
            }
        }
    

    Using this looks like:

    GridUtility.SaveSorting(grid);    
    grid.DataSource = databaseFetch(); // or whatever
    GridUtility.RestoreSorting(grid);
    
    0 讨论(0)
  • 2021-02-10 21:31

    I came across Adam Nofsinger's answer before I encounterd the problem, but I used it anyway. It works wonderfull.

    Just needed to add these 2 using lines to my class file:

    using System.ComponentModel; 
    using System.Windows.Forms;
    

    Thanks, Geert.

    0 讨论(0)
提交回复
热议问题