private async void btnCheckin_Click(object sender, RoutedEventArgs e) { string selected = (string)this.checkinComboBox.SelectedValue; try { if (!selected.Trim().Equals("")) { officer selectedOfficer = this.officers.Where(p => p.officer_id.Equals(selected)).First(); checkin_history tmpHistory = new checkin_history(); tmpHistory.officer_id = selected; tmpHistory.officer_name = selectedOfficer.officer_name; tmpHistory.term = MainWindow.term; tmpHistory.insert_date = DateTime.Now; using (check_dorm_newEntities db = new check_dorm_newEntities()) { db.checkin_history.Add(tmpHistory); db.SaveChanges(); this.checkedOfficers.Add(selectedOfficer); this.checkinDataGrid.ItemsSource = null; this.checkinDataGrid.ItemsSource = this.checkedOfficers; } await this.ShowMessageAsync("", "签到成功!"); } } catch (Exception exp) { } }
private async void btnRemoveRecord_Click(object sender, RoutedEventArgs e) { try { if (this.management == Management.人员) { // 删除的是人员数据 officer tmpOfficer = (officer)this.officeGrid.SelectedItem; using (check_dorm_newEntities db = new check_dorm_newEntities()) { MessageDialogResult result = await this.ShowMessageAsync("人员管理", "您确定要删除该行数据吗", MessageDialogStyle.AffirmativeAndNegative); if (result != MessageDialogResult.Negative) { // 删除 db.officer.Remove(db.officer.Where(p => p.row_id.Equals(tmpOfficer.row_id)).First()); db.SaveChanges(); this.officers.Remove(tmpOfficer); this.officeGrid.ItemsSource = null; this.officeGrid.ItemsSource = this.officers; } } } else if (this.management == Management.楼栋) { // 删除的是楼栋数据 dorm tmpDorm = (dorm)this.dormGrid.SelectedItem; using (check_dorm_newEntities db = new check_dorm_newEntities()) { MessageDialogResult result = await this.ShowMessageAsync("楼栋管理", "您确定要删除该行数据吗", MessageDialogStyle.AffirmativeAndNegative); if (result != MessageDialogResult.Negative) { // 删除 db.dorm.Remove(db.dorm.Where(p => p.dorm_name.Equals(tmpDorm.dorm_name)).First()); db.SaveChanges(); this.dorms.Remove(tmpDorm); this.dormGrid.ItemsSource = null; this.dormGrid.ItemsSource = this.dorms; } } } } catch (Exception exp) { } }
private void officeGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { // 获取编辑后的数据 officer tmpOfficer = (officer)e.Row.DataContext; if (e.EditAction == DataGridEditAction.Commit) { try { using (check_dorm_newEntities db = new check_dorm_newEntities()) { if (tmpOfficer.row_id.Equals(0)) { // row_id == 0说明是新加入的行 officer newOfficer = new officer(); newOfficer.officer_id = tmpOfficer.officer_id; newOfficer.officer_department = tmpOfficer.officer_department; newOfficer.officer_name = tmpOfficer.officer_name; newOfficer.officer_gender = tmpOfficer.officer_gender; db.officer.Add(newOfficer); db.SaveChanges(); // 添加完毕以后需要重新读取数据库中新加入行的row_id用于更新内存中缓存的row_id this.officers.Where(p => p.officer_id.Equals(newOfficer.officer_id)).First().row_id = db.officer.Where(p => p.officer_id.Equals(newOfficer.officer_id)).First().row_id; } else { // row_id != 0说明是原先存在的行,修改即可 db.officer.Where(p => p.row_id.Equals(tmpOfficer.row_id)).First().officer_id = tmpOfficer.officer_id; db.officer.Where(p => p.row_id.Equals(tmpOfficer.row_id)).First().officer_department = tmpOfficer.officer_department; db.officer.Where(p => p.row_id.Equals(tmpOfficer.row_id)).First().officer_name = tmpOfficer.officer_name; db.officer.Where(p => p.row_id.Equals(tmpOfficer.row_id)).First().officer_gender = tmpOfficer.officer_gender; db.SaveChanges(); } } } catch (Exception exp) { e.Cancel = true; (sender as DataGrid).CancelEdit(DataGridEditingUnit.Row); } } }