private void btnAddBattalion_Click(object sender, RoutedEventArgs e) { if (lbMilitaryUnit.SelectedItem != null) { int idMilitaryUnit = (lbMilitaryUnit.SelectedItem as MilitaryUnit).Id; SubUnit s = addSubUnit.getNewSubUnit(idMilitaryUnit, -1); if (s != null) { if (bList == null) { bList = new SubUnitList(); } int id = sqlWorker.getNewId("SubUnit"); s.Id = id; if (sqlWorker.addSubUnit(id, s)) { bList.values.Add(s); } else { MessageBox.Show("Ошибка при добавлении элемента"); } } } }
public static SubUnit getEditedSubUnit(SubUnit s) { addSubUnit wndS = new addSubUnit(s); wndS.ShowDialog(); return(wndS.subUnit); }
private void btnOk_Click(object sender, RoutedEventArgs e) { if (txName.Text != "" && txShortName.Text != "") { if (subUnit == null) { subUnit = new SubUnit( 0, txName.Text, txShortName.Text, txQuantity.Text == "" ? 0 : Int32.Parse(txQuantity.Text), idFKSubUnit, idMilitaryUnit); } else { subUnit.Name = txName.Text; subUnit.ShortName = txShortName.Text; subUnit.Quantity = txQuantity.Text == "" ? 0 : Int32.Parse(txQuantity.Text); } this.Close(); } else { MessageBox.Show("Не заполнены поля полноги и(или) краткого наименования!"); } }
internal static ObservableCollection <SubUnit> getSubUnitList(DataTable table) { ObservableCollection <SubUnit> list = new ObservableCollection <SubUnit>(); if (table.Rows.Count > 0 && table.Columns.Count == 6) { try { for (int i = 0; i < table.Rows.Count; i++) { SubUnit r = new SubUnit( Int32.Parse(table.Rows[i][0].ToString()), table.Rows[i][1].ToString(), table.Rows[i][2].ToString(), table.Rows[i][3].ToString() == "" ? 0 : Int32.Parse(table.Rows[i][3].ToString()), Int32.Parse(table.Rows[i][3].ToString() == "" ? "0" : table.Rows[i][4].ToString()), Int32.Parse(table.Rows[i][3].ToString() == "" ? "0" : table.Rows[i][5].ToString())); list.Add(r); } } catch (Exception ex) { throw new Exception("Некорректные данные в БД:\n" + ex.Message); } } return(list); }
public addSubUnit(SubUnit s) { InitializeComponent(); txName.Focus(); subUnit = s; txName.Text = s.Name; txShortName.Text = s.ShortName; txQuantity.Text = s.Quantity.ToString(); }
internal void update(SubUnit s) { for (int i = 0; i < values.Count; i++) { if (values[i].Id == s.Id) { values[i].Name = s.Name; values[i].ShortName = s.ShortName; values[i].Quantity = s.Quantity; } } }
internal bool updateSubUnit(SubUnit s) { try { executeQuery("UPDATE SubUnit SET name = '" + s.Name + "', shortName = '" + s.ShortName + "', quantity = " + (s.Quantity == 0?"NULL":s.Quantity.ToString()) + " WHERE idSubUnit = " + s.Id + ";"); return(true); } catch { return(false); } }
private void lbBattalion_MouseDoubleClick(object sender, MouseButtonEventArgs e) { if (lbMilitaryUnit.SelectedItem != null) { SubUnit s = addSubUnit.getEditedSubUnit(lbBattalion.SelectedItem as SubUnit); if (s != null) { if (sqlWorker.updateSubUnit(s)) { bList.update(s); lbBattalion.Items.Refresh(); } else { MessageBox.Show("Ошибка при изменении элемента"); } } } }
internal bool addSubUnit(int id, SubUnit newItem) { try { executeQuery("INSERT INTO SubUnit VALUES(" + id + ",'" + newItem.Name + "','" + newItem.ShortName + "'," + (newItem.Quantity == 0 ? "NULL" : newItem.Quantity.ToString()) + "," + (newItem.IdFKSubUnit == -1 ? "NULL" : newItem.IdFKSubUnit.ToString()) + "," + (newItem.IdMilitaryUnit == -1 ? "NULL" : newItem.IdMilitaryUnit.ToString()) + ");"); return(true); } catch { return(false); } }