13/08/13

Insert Update Delete mVVm

Create model folder,
1)  public class EmployeeDTO
    {    
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime HireDate { get; set; }
        public int Title { get; set; }
    }

2)
    public class GetData
    {
        public List<EmployeeDTO> employees;
        public List<EmployeeDTO> GetEmployees()
        {

            employees = new List<EmployeeDTO>();
            EmployeeDTO employee = new EmployeeDTO();
            employee.ID = 1;
            employee.FirstName = "Jason";
            employee.LastName = "Apergis";
            employees.Add(employee);

            employee = new EmployeeDTO();
            employee.ID = 2;
            employee.FirstName = "Ethan";
            employee.LastName = "Apergis";
            employees.Add(employee);

            employee = new EmployeeDTO();
            employee.ID = 3;
            employee.FirstName = "Caroline";
            employee.LastName = "Apergis";
            employees.Add(employee);

            return employees;
        }

        private static int count = 10;
        public void InsertEmployeeDTO(EmployeeDTO employee)
        {
            //Code to insert employee
            employee.ID = count++;
            employees.Add(employee);
        }

        public void UpdateEmployeeDTO(EmployeeDTO employee)
        {
            //Code to update employee

        }

        public void DeleteEmployeeDTO(EmployeeDTO employee)
        {
            //Code to delete employee
            employees.Remove(employee);
        }
    }

3) Create View Model folder Inside it delete command , insert command, update command, empoyee view model
  public class DeleteCommand  : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private EmployeeViewModel _evm = null;

        public DeleteCommand(EmployeeViewModel evm)
        {
            _evm = evm;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _evm.Delete();
        }
    }  

 public class InsertCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private EmployeeViewModel _evm = null;

        public InsertCommand(EmployeeViewModel evm)
        {
            _evm = evm;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _evm.Insert();
        }
    }

 public class SaveCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;
        private EmployeeViewModel _evm = null;

        public SaveCommand(EmployeeViewModel evm)
        {
            _evm = evm;
        }

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            _evm.Save();
        }
    }

ViewModel---------------------------
 public class EmployeeViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private GetData _context = null;
        private EmployeeDTO _selectedEmployee = null;

        public EmployeeViewModel()
        {
            _context = new GetData();
            _context.GetEmployees();
        }

        public EmployeeDTO SelectedEmployee
        {
            get
            {
                return _selectedEmployee;
            }
            set
            {
                _selectedEmployee = value;
                RaisePropertyChanged("SelectedEmployee");
            }
        }

        public IList<EmployeeDTO> Employees
        {
            get
            {
                return _context.employees;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                return new SaveCommand(this);
            }
        }

        public void Save()
        {
            if (_context.employees != null)
            {              
                RaisePropertyChanged("Employees");
            }
        }

        public ICommand InsertCommand
        {
            get
            {
                return new InsertCommand(this);
            }
        }

        public void Insert()
        {

            EmployeeDTO employee = new EmployeeDTO();
            employee.ID = 0;
            _context.InsertEmployeeDTO(employee);
            RaisePropertyChanged("Employees");
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new DeleteCommand(this);
            }
        }

        public void Delete()
        {
            _context.employees.Remove(SelectedEmployee);
            RaisePropertyChanged("Employees");
        }

        private void RaisePropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
    }

MainWindow Xaml
 <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" x:Name="employeeview" Background="White">
            <DataGrid AutoGenerateColumns="True" Name="grdEmployees"
                      Height="168" VerticalAlignment="Top"
                      HorizontalAlignment="Left" Width="400"
                      ItemsSource="{Binding Employees}"
                      SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}"/>
            <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="232,176,0,0"
                Name="btnInsert" VerticalAlignment="Top" Width="75"
                Command="{Binding InsertCommand}"/>
            <Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="313,176,0,0"
                Name="btnDelete" VerticalAlignment="Top" Width="75"
                Command="{Binding DeleteCommand}"/>
       
        </Grid>
        <Grid Grid.Row="1" x:Name="updateview" Background="White">
            <TextBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0"
                 Name="txtFirstName" VerticalAlignment="Top" Width="120"
                 Text="{Binding SelectedEmployee.FirstName, Mode=TwoWay}"/>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="138,12,0,0"
                 Name="txtLastName" VerticalAlignment="Top" Width="120"
                 Text="{Binding SelectedEmployee.LastName, Mode=TwoWay}"/>
            <Button Content="Save" Height="23" HorizontalAlignment="Left"
                Margin="264,12,0,0" Name="btnSave" VerticalAlignment="Top"
                Width="75" Command="{Binding SaveCommand}"/>
        </Grid>
    </Grid>

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            EmployeeViewModel emv = new EmployeeViewModel();
            employeeview.DataContext = emv;
            updateview.DataContext = emv;
        }
    }

No comments:

Post a Comment