예제 #1
0
        private void dgData_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            EmployeeDBDataContext context = new EmployeeDBDataContext();
            Employee employee             = dgData.SelectedItem as Employee;

            if (employee != null)
            {
                var matchedCustomer = (from em in context.GetTable <Employee>()
                                       where em.ID == employee.ID
                                       select em).SingleOrDefault();
                if (e.Command == DataGrid.DeleteCommand)
                {
                    if (!(MessageBox.Show("Are You Sure you want to Delete ?",
                                          "Confirm Delete !", MessageBoxButton.YesNo) == MessageBoxResult.Yes))
                    {
                        e.Handled = true;
                    }
                    else
                    {
                        context.Employees.DeleteOnSubmit(matchedCustomer);
                        context.SubmitChanges();
                        txtStatus.Text = "Success: Selected Data Deleted.";
                    }
                }
            }
        }
예제 #2
0
        private void dgData_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            if (e.EditAction == DataGridEditAction.Commit)
            {
                EmployeeDBDataContext context = new EmployeeDBDataContext();
                Employee emp = e.Row.DataContext as Employee;

                var matchedData = (from em in context.GetTable <Employee>()
                                   where em.ID == emp.ID
                                   select em).SingleOrDefault();

                if (matchedData == null)
                {
                    Table <Employee> empTable = context.GetTable <Employee>();

                    Employee employee = new Employee();
                    employee.FirstName = emp.FirstName;
                    employee.LastName  = emp.LastName;
                    employee.EmailID   = emp.EmailID;
                    employee.Contact   = emp.Contact;

                    empTable.InsertOnSubmit(employee);
                    empTable.Context.SubmitChanges();

                    txtStatus.Text = "Success: Data Inserted";
                }
                else
                {
                    matchedData.FirstName = emp.FirstName;
                    matchedData.LastName  = emp.LastName;
                    matchedData.EmailID   = emp.EmailID;
                    matchedData.Contact   = emp.Contact;
                    context.SubmitChanges();

                    txtStatus.Text = "Success: Data Updated";
                }
            }
        }