Why DataGridComboBoxColumn's ItemsSource will refresh after DataGrid reload ?

Mojtaba_Hakim 281 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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,670 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,226 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hui Liu-MSFT 38,191 Reputation points Microsoft Vendor
    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.