Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
 
After adding a new Customer ,it is OK in my database but i want to refresh the DataGrid,


XAML:
XML
<pre><DataGrid x:Name="EmpDataGrid"
                       
                      ItemsSource="{Binding loadDataBinding,Mode=TwoWay}"                   
                      CanUserResizeRows="False"
                      CanUserAddRows="False"
                      SelectedItem="{Binding CurrentCustomer}" Grid.ColumnSpan="2">


ViewModel:



When i finish he function ADD, the Datagrid does not refresh !! and the Customer in my database is added


Thanks

What I have tried:

<pre lang="c#"><pre>        private ObservableCollection<Custmor> _loadDataBinding;    

        public ObservableCollection<Custmor> loadDataBinding
        {
            get
            {
                return _loadDataBinding;
            }
            set
            {  _loadDataBinding = value;
                OnPropertyChanged("loadDataBinding");
            }
        }
		
		
		
		 public ViewModel1()
        {
          //Display my DataGrid 
            using (Test1Entities context = new Test1Entities())
            {
                _loadDataBinding = new ObservableCollection<Custmor>(context.Custmor.ToList());
            }  
               
			   //To Show the Window ADD from Window MainWindow
            addCustomerMainWindow = new RelayCommand(add);          

            CustomerToAddObject = new Custmor();

            addCustomer1 = new RelayCommand(ADDFunction);         

		}
		
		//To Show the Window ADD from Window MainWindow
		  private ICommand addCustomerMainWindow;
          public ICommand AddCustomerMainWindow
        {
            get { return addCustomerMainWindow; }
        }

        private void add(object obj)
        {
          
            Add addView = new Add();           
            addView.Show();            
        }
		
		
		private Custmor customerToAddObject;

        public Custmor CustomerToAddObject
        {
            get { return customerToAddObject; }
            set { customerToAddObject = value; }
        }
		
		//The Button ADD from Window ADD
		
		private ICommand addCustomer1;
        public ICommand AddCustomer1
        {
            get { return addCustomer1; }
        }

        private void ADDFunction(object obj)
        {
            using (Test1Entities context = new Test1Entities())
            {
                context.Custmor.Add(customerToAddObject);
                context.SaveChanges();
            }
            
            _loadDataBinding.Add(CurrentCustomer);

        }
Posted
Updated 22-May-17 1:44am

1 solution

Hi again,

in your ADDFunction, you are adding CurrentCustomer to your collection. I guess it's a typo and it should be customerToAddObject object you are adding to your database.

Also, you may want to create an object in the time of adding, not in your constructor.

private void ADDFunction(object obj)
{
    // create new object before adding
    customerToAddObject = new Custmor();
    using (Test1Entities context = new Test1Entities())
    {
        context.Custmor.Add(customerToAddObject);
        context.SaveChanges();
    }
    
    // add correct instance to your binded collection
    _loadDataBinding.Add(customerToAddObject);
}
 
Share this answer
 
v2
Comments
ThabetMicrosoft 22-May-17 8:03am    
Thanks,but always the DataGrid doesn't refresh

* this is XAML Window ADD: Add

always the new window of Adding is still!
It must close after addin and refresh the Datagrid ? but How do it?
jimmson 22-May-17 8:10am    
Oh yeah, I see it now. The problem is that the collection in this window is different from the collection bound to your DataGrid.
It's not clear to me how you create this window, but you can resolve the problem if you pass the instance of _loadDataBinding from the previous window where the DataGrid is to this window and use it, instead of re-creating it.
ThabetMicrosoft 22-May-17 12:21pm    
thanks for your answer ,
it refresh now but just for the one adding new Customer..i can't add many..
thanks ..i will ask new question about this new error

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