We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

How to refresh sfDataGrid items

Hi,
In my WPF app, I have a simple sfdatagrid that I bind to "EditedForms" collection as shown below.
        <syncfusion:SfDataGrid x:Name="gridSaved" Margin="452,10,10,0" VerticalAlignment="Top" Height="405"
                AutoGenerateColumns="False" 
                ColumnSizer="Auto" 
                ItemsSource="{Binding EditedForms}">
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn HeaderText="Form" MappingName="Name" />
            </syncfusion:SfDataGrid.Columns>
        </syncfusion:SfDataGrid>
When I delete a file from the folder, I update the "EditedForms" collection. 
        public void RefreshSavedFolder() {
            EditedForms.Clear();
            DirectoryInfo dInfo = new DirectoryInfo(SavedPath);
            foreach (FileInfo file in dInfo.GetFiles()) {
                EditedForms.Add(new FormTemplate(file.Namefile.DirectoryNamefile.FullName));
            }
How do I refresh this grid with the new collection on button click event?

Thanks,
Biju

5 Replies

GM Gobikrishnan Murugesan Syncfusion Team April 27, 2015 08:34 AM UTC

Hi Biju Vargheese,

Thank you for using Syncfusion products.

We have analyzed your query. Based on your code snippet, we are unable to find the collection type you are using whether it is List, ObservableCollection. If you are using ObservableCollection which implements INotifyCollectionChanged interface, SfDataGrid will be refreshed for Add and Remove operations in collection. If you are using List collection which does not implement the INotifyCollectionChanged interface, you can manually refresh the using below code snippet (using SfDataGrid.View.BeginInit() , SfDataGrid.View.EndInit() and SfDataGrid.View.Refresh() methods)

Code Snippet for Refresh the View if you are using List:

//When BeginInit method is called it suspends all the updates until EndInit method is called

sfdatagrid.View.BeginInit();

viewmodel.Products.Clear();

viewmodel.Products.Add(new Product("P1", "HardWare", 1, "Jon"));

viewmodel.Products.Add(new Product("P2", "Software", 2, "Jhon"));

viewmodel.Products.Add(new Product("P3", "HardWare", 3, "Michel"));

sfdatagrid.View.EndInit();

sfdatagrid.View.Refresh();


Please let us know if you have any other queries.


Regards,

Gobikrishnan M



BV Biju Vargheese April 27, 2015 03:21 PM UTC

Hi Gopi,
Thanks for your fast response! I am using a collection of FormTemplate objects.  FormTemplate is a simple class with two properties: Name and Category.
public ObservableCollection<FormTemplate> EditedForms { getprivate set; }
My code adds a new item to the collection. But I was expecting the grid to auto-refresh and display it. I might be missing something... Any idea?
            foreach (FileInfo file in dInfo.GetFiles()) {
                EditedForms.Add(new FormTemplate(file.Name"Saved"file.DirectoryName));
            }
Thanks,
Biju


GM Gobikrishnan Murugesan Syncfusion Team April 28, 2015 01:37 PM UTC

Hi Biju,


We have analyzed your query. As mentioned in our previous update, in Observable collection, the collection itself implements the INotifyCollectionChanged interface. So SfDataGrid will be refreshed automatically for Add, Remove and Clear operations in collection. Based on your code snippet, we have prepared a sample and it can be downloaded from the following location.


http://www.syncfusion.com/downloads/support/forum/118940/ObservableCollection_SfGrid_WPF1584462495.zip


In this sample, we have performed the operations like clearing the items and add new items in collection as you updated, but our SfDataGrid refreshed automatically. Could you please update more information on reported problem like modifying the above sample or provide issue reproducing sample to us? It would be better for us and provide a better solution.


Please let us know if you require further assistance on this.


Regards,

Gobikrishnan M



BV Biju Vargheese April 28, 2015 03:49 PM UTC

Thanks Gobi,
That was helpful. My code was directly modifying the collection instead of getting it from the grid.DataContext.

Fix:
            var formUser = (FormUsergridSaved.DataContext;
            formUser.EditedForms.Clear();
Thanks,
Biju




GM Gobikrishnan Murugesan Syncfusion Team April 29, 2015 07:05 AM UTC

Hi Biju,

Thanks for the update.
Please let us know if you require any further assistance on this.

Regards,
Gobikrishnan M

Loader.
Live Chat Icon For mobile
Up arrow icon