Why DataGridComboBoxColumn's ItemsSource will refresh after DataGrid reload ?

Mojtaba_Hakim 321 Reputation points
2022-09-06T12:22:44.37+00:00

I have C# WPF project that is save data from DataGrid into a Table in SQL Server Database

In Database I have two tables as Factor and Commodity that related to each other and

-The ItemsSource of the DatGrid is filled from the Factor table
-The ItemsSource of the DataGridComboBoxColumn is filled with Commoditiy (the Item source of DataGrid and comboBoxColumn are diffrent)

When I want to Realod DataGrid's Data by "select * from Factor" I Only Reloaded the DataGrid's ItemsSource not The ItemsSource of the DataGridComboBoxColumn
why DataGridComboBoxColumn's ItemsSource will refresh (as if moving on each item)

Datagrid-Refresh-1.gif

full code:how-can-i-prevent-the-observablecollection-from-being-refreshed-for-no-reason-in

Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-09-08T09:05:47.513+00:00

    A very quick and easy way is to subclass ObservableCollection and suspend notifications when AddRange is called. You could try to refer to it according to your needs.

     public class MyObservableCollection<T> : ObservableCollection<T>  
    	{  
    		private bool myNotification = false;  
      
    		protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)  
    		{  
    			if (!myNotification)  
    				base.OnCollectionChanged(e);  
    		}  
      
    		public void AddRange(IEnumerable<T> list)  
    		{  
    			if (list == null)  
    				throw new ArgumentNullException("list");  
      
    			myNotification = true;  
      
    			foreach (T item in list)  
    			{  
    				Add(item);  
    			}  
    			myNotification = false;  
    			OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));  
    		}  
    	}  
    

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.