Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I am facing a small problem. I have made simple MVVM application which load data from Database and shows it in a grid. I have added Delete and Edit buttons. I am currently working on Delete Button. Have binded it to DeleteCommand which deletes the selected Employee. It is actually deleted from DB , but the UI is not updating. I have implemented INotifyPropertyChanged.


Below is my View Model
C#
using SPDemo.Commands;
using SPDemo.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SPDemo.ViewModels
{
    public class EmployeeViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Employee> _employeesList;
        public ObservableCollection<Employee> employeesList 
        { 
            get
            {
                return _employeesList;
            }
            set
            {
                _employeesList = value;
                OnPropertyChanged("employeesList");
            }
        }
        public EmployeeViewModel()
        {
            Employee emp = new Employee();
            employeesList = emp.GetAllEmployees();
            DeleteCommand = new RelayCommand(DeleteSelected);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }

        public ICommand DeleteCommand { get; set; }

        private void DeleteSelected(object state)
        {
            if (null != state)
            {
                Employee emp = new Employee();
                emp.RemoveEmployee(state as Employee);
                employeesList = emp.GetAllEmployees();
                OnPropertyChanged("employeeList");
            }
        }

    }
}



and here is the ViewCode

HTML
<Window x:Class="SPDemo.Views.EmployeeView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:SPDemo.ViewModels"
        Title="Employees" Height="300" Width="500">
    <Window.Resources>
        <my:EmployeeViewModel x:Key="vmCommands"></my:EmployeeViewModel>
    </Window.Resources>
    <Window.DataContext>
        <my:EmployeeViewModel></my:EmployeeViewModel>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DataGrid ItemsSource="{Binding employeesList,UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" SelectedValue="{Binding SelectedRow,Mode=TwoWay}" CanUserAddRows="False" CanUserDeleteRows="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name,Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Address" Binding="{Binding Address,Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Salary" Binding="{Binding Salary,Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="MobileNo" Binding="{Binding MobileNo,Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTextColumn Header="Qualification" Binding="{Binding Qualification,Mode=TwoWay}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Actions">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Button Content="Delete" Foreground="Red" Margin="0,0,5,0" Command="{Binding Source={StaticResource vmCommands},Path=DeleteCommand}" CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"></Button>
                                <Button Content="Edit" Foreground="Black"></Button>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
Posted
Comments
Kannan Subramaniam 14-Sep-15 23:45pm    
private void DeleteSelected(object state)
{
if (null != state)
{
Employee emp = new Employee();
emp.RemoveEmployee(state as Employee);
//employeesList = emp.GetAllEmployees(); // Remove this code and try.
OnPropertyChanged("employeeList");
}
}

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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