Refresh contents of DataGrid's Data View

Posted by: msloan on 12 June 2018, 11:24 am EST

    • Post Options:
    • Link

    Posted 12 June 2018, 11:24 am EST

    I have a form with a C1DataGrid on it. When I double-click a row I display another form which allows the user to edit the record. When the user saves the changes to the record, how can I refresh the Data grid so that changes to the record are reflected on the grid.

    Currently, I re-run the query used to populate the data grid but this has the effect of clearing any filters and losing the selected row.

    Is there a better way which preserves the filters and the selected row?

  • Posted 13 June 2018, 7:38 am EST

    I have found the following seems to work fine:

    
            List<C1.WPF.DataGrid.DataGridColumn> M_lstCurrentFilteredColumns = new List<C1.WPF.DataGrid.DataGridColumn>();
            private int M_intSavedSelectedIndex = -1;
    
            /// <summary>
            /// Refresh Grid
            /// </summary>
            /// <param name="P_boolRetainSelection"></param>
            public void RefreshGrid(bool P_boolRetainSelection = false)
            {
                //Save current Selected Index values
                M_intSavedSelectedIndex = M_dgComOne.SelectedIndex;
    
                //Save current set of filters
                SaveFilters();
    
                //Rerun current Query
                DataToView = M_strQuery;
    
                //Restore current set of filters
                RestoreFilters();
    
                //Restore current Selected Index, if required
                if (P_boolRetainSelection)
                {
                    M_dgComOne.SelectedIndex = M_intSavedSelectedIndex;
                }
            }
    
            /// <summary>
            /// Save the current set of filtered columns
            /// </summary>
            public void SaveFilters()
            {
                //Save current set of filters
                M_lstCurrentFilteredColumns.Clear();
                foreach (C1.WPF.DataGrid.DataGridColumn L_objColumn in M_dgComOne.FilteredColumns)
                {
                    M_lstCurrentFilteredColumns.Add(L_objColumn);
                }
            }
    
            /// <summary>
            /// Save the current set of filtered columns
            /// </summary>
            public void RestoreFilters()
            {
                //Restore current set of filters
                foreach (C1.WPF.DataGrid.DataGridColumn L_objColumn in M_dgComOne.Columns)
                {
                    foreach (C1.WPF.DataGrid.DataGridColumn L_objFilteredColumn in M_lstCurrentFilteredColumns)
                    {
                        if (L_objColumn.Name == L_objFilteredColumn.Name)
                        {
                            //Reapply Filter
                            DataGridFilterState L_objFilterState = new DataGridFilterState();
                            L_objFilterState = L_objFilteredColumn.FilterState;
                            M_dgComOne.FilterBy(M_dgComOne.Columns[L_objFilteredColumn.Name], L_objFilterState, true);
                            break;
                        }
                    }
                }
            }
    
    
  • Posted 13 June 2018, 9:17 am EST

    Hello,

    Glad to see that you were able to figure out a solution for yourself.

    Though, the code you developed works fine for Filter, but other settings like Grouping, Sorting etc would still get reset. So, I suggest you to take a look at product sample application ‘C1DataGrid_SaveState’ installed at \Documents\ComponentOne Samples\WPF\C1.WPF.DataGrid\CS location in your machine to learn how to save state.

    Hopefully it would help you.

    Thanks,

    Ruchir

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels