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.
Unfortunately, activation email could not send to your email. Please try again.

How to refresh sfDataGrid items

Thread ID:

Created:

Updated:

Platform:

Replies:

118940 Apr 26,2015 07:09 PM Apr 29,2015 03:05 AM WPF 5
loading
Tags: SfDataGrid
Biju Vargheese
Asked On April 26, 2015 07:09 PM

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

Gobikrishnan Murugesan [Syncfusion]
Replied On April 27, 2015 04:34 AM

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


Biju Vargheese
Replied On April 27, 2015 11:21 AM

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

Gobikrishnan Murugesan [Syncfusion]
Replied On April 28, 2015 09:37 AM

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


Biju Vargheese
Replied On April 28, 2015 11:49 AM

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



Gobikrishnan Murugesan [Syncfusion]
Replied On April 29, 2015 03:05 AM

Hi Biju,

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

Regards,
Gobikrishnan M

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;