Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to implement an "undo" button(RevertChanges method) in my program.

Everything seems to work fine, but when I call the RevertChanges void (It´s binded to a button), my LINQ2SQL object is correctly reverted, but the DataGrid control doesn't get updated.

Only when I click twice a cell or force to refresh (Using datagrid.Items.Refresh()) the content of the DataGrid take the actual value.

I'm new programming C#, so maybe it's just a basic mistake.

The LINQtoSQL class was generater automatically.

My ViewModel class:

C#
    public class ViewModel : INotifyPropertyChanged{

       
        public static DataClasses1DataContext DContext1 = new DataClasses1DataContext();
    
        public ViewModel()
        {
            Model Model1 = new Model();
            tblToView = Model1.GetList(DContext1);
            var query = from a in Model1.GetList(DContext1) select a;
            ListTest1 = new ObservableCollection<utblTestTable>(query.ToList());
          
        }
        public ViewModel(int itemNo)
        {
            Model Model1 = new Model();
            tblToView = Model1.GetList(DContext1,itemNo);
        }

        public ObservableCollection<utblTestTable> ListTest1 {get;set;}
        public IEnumerable<utblTestTable> tblToView;        
        private RelayCommand updatecmd1;
        public RelayCommand UpdateCMD1
        {
            get
            {
                updatecmd1 = new RelayCommand(a => UpdateVoid1());
                return updatecmd1;
            }
        }

        private RelayCommand newwindowcmd1;
        public RelayCommand NewWindowCMD1
        {
            get
            {
                newwindowcmd1 = new RelayCommand(a => NewWindow());
                return newwindowcmd1;
            }
        }

        private RelayCommand savecmd1;
        public RelayCommand SaveCMD1
        {
            get
            {
                savecmd1 = new RelayCommand(a => RevertChanges(a));
                return savecmd1;
            }
        }

        private void UpdateVoid1()
        {
            DContext1.SubmitChanges();
            
        }

        private void RevertChanges(object parameter)
        {
            DContext1.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DContext1.utblTestTables);
        }

        private int item;
        public utblTestTable Item
        {
            set
            {
                item = value.ID;
            }
        }

     
        private void NewWindow()
        {
            ViewModel VMPopUp = new ViewModel(item);
            ViewPopup1 PupUpView = new ViewPopup1();
            PupUpView.DataContext = this;
            PupUpView.dGridPopup.ItemsSource = VMPopUp.tblToView;
            PupUpView.ShowDialog();
         
        }


        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}


And the XAML code ot the DataGrid binding:

XML
<DataGrid ItemsSource="{Binding ListTest1, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" CanUserResizeRows="False" SelectedItem ="{Binding Item , UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource PlaniTDGrid}" Name="dGrid1" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="386" Width="772">
Posted
Comments
Maciej Los 25-May-15 13:39pm    
Have you tried to debug the programm?
Member 11615151 26-May-15 3:28am    
Sure, when I try to revert the changes, the utbTestTable class is called, going to the "get" part of each field.

I think that the problem is being caused because the SendPropertyChanged isn´t being sended when the table is updated using the linq refresh method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900