problem with crud operations, when i add new item to observablecollection is not sync to datagrid
Loading
problem with crud operations, when i add new item to observablecollection is not sync to datagrid
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 1, 2022, 5:39 AM EDT
Test it
MainWindow.xaml:MainViewModel.cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Runtime.CompilerServices; using System.Windows.Input; namespace ObservableCollectionDemo; public class MainViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; public ObservableCollection Data { get; set; } public ICommand AddDataCommand { get; set; } public MainViewModel() { Random r = new Random(); Data = new ObservableCollection(); AddDataCommand = new RelayCommand(() => { Data data = new Data { Col1 = r.Next(), Col2 = r.Next(), Col3 = r.Next() }; Data.Add(data); }); } protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } protected bool SetField(ref T field, T value, [CallerMemberName] string? propertyName = null) { if (EqualityComparer.Default.Equals(field, value)) return false; field = value; OnPropertyChanged(propertyName); return true; } } public class Data { public int Col1 { get; set; } public int Col2 { get; set; } public int Col3 { get; set; } } public class RelayCommand : ICommand { private readonly Action _action; public RelayCommand(Action action) { _action = action; } public bool CanExecute(object? parameter) { return true; } public void Execute(object? parameter) { _action.Invoke(); } public event EventHandler? CanExecuteChanged; }