//инициализация новой строки private void x_MainGrid_AddingNewItem(object sender, AddingNewItemEventArgs e) { try { newItem = new CategoryPatient(); newItem.Id = 0; newItem.Name = ""; e.NewItem = newItem; } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); } }
//заполнение списка private void FillList() { try { var tab = from t1 in context.d_Category_of_patient select new { Id = t1.id, Name = t1.category }; foreach (var item in tab) { CategoryPatient row = new CategoryPatient(); row.Id = item.Id; row.Name = item.Name; ListItems.Add(row); } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); } }
//добавление новой строки private void x_MainGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { if (newItem == null) { return; } try { if (newItem.Name == "") { MessageBox.Show("Заповніть поле \"Назва\""); return; } var col = context.d_Category_of_patient.Where(c => c.category == newItem.Name); if (col.Count() != 0) { newItem.Name = ""; MessageBox.Show("Група з такою назвою вже існує"); return; } d_Category_of_patient newRow = new d_Category_of_patient(); newRow.category = newItem.Name; context.d_Category_of_patient.Add(newRow); context.SaveChanges(); newItem.Id = newRow.id; newItem = null; } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace); } }