private void button3_Click(object sender, EventArgs e) { using (DbOption db = new DbOption()) { var NewOption = db.UserOptions.Where(x => x.UserName == UserComboBox.Text).FirstOrDefault(); if (LoginTextBox.Text != "" & PasswordTextBox.Text != "" & ProxyTextBox.Text != "" & PortTextBox.Text != "" & UserComboBox.Text != "" & DirectoryTextBox.Text != "") { if (NewOption == null) { db.UserOptions.Add(new UserOptions { Login = LoginTextBox.Text, Password = PasswordTextBox.Text, Port = Convert.ToInt32(PortTextBox.Text), Proxy = ProxyTextBox.Text, UserName = UserComboBox.Text, Directory = DirectoryTextBox.Text }); db.SaveChanges(); } else { db.UserOptions.Remove(NewOption); db.UserOptions.Add(new UserOptions { Login = LoginTextBox.Text, Password = PasswordTextBox.Text, Port = Convert.ToInt32(PortTextBox.Text), Proxy = ProxyTextBox.Text, UserName = UserComboBox.Text, Directory = DirectoryTextBox.Text }); db.SaveChanges(); } } else { MessageBox.Show("Заполните все поля, чтобы отредактировать данные, или добавить нового пользователя"); } } }
private void button2_Click(object sender, EventArgs e) { List <Units> SaveUnit = new List <Units>(); List <Units> RemoveUnit = new List <Units>(); for (int i = CheckedUnits.Items.Count - 1; i >= 0; i--) { if (!CheckedUnits.GetItemChecked(i)) { SaveUnit.Add((Units)CheckedUnits.Items[i]); } else { RemoveUnit.Add((Units)CheckedUnits.Items[i]); } } SaveUnit.Reverse(); CheckedUnits.DataSource = SaveUnit; CheckedUnits.DisplayMember = "Name"; CheckedUnits.ValueMember = "UnitId"; using (DbOption db = new DbOption()) { foreach (Units a in RemoveUnit) { var unit = db.Units.Where(u => u.Name == a.Name & u.UnitId == a.UnitId & u.GroupId == a.GroupId).First(); db.Units.Remove(unit); db.SaveChanges(); } } }
private void button1_Click(object sender, EventArgs e) { List <Units> NewUnits = new List <Units>(); foreach (Units a in CheckedUnits.Items) { NewUnits.Add(a); } if (ComboBoxGroup.SelectedValue != null & ComboBoxGroup.SelectedValue.ToString() != "") { foreach (Units Check in UnitsWialon.CheckedItems) { bool flag = false; foreach (Units i in CheckedUnits.Items) { if (Check.Name == i.Name) { flag = true; break; } } if (!flag) { NewUnits.Add(new Units() { Name = Check.Name, UnitId = Check.UnitId, GroupId = Convert.ToInt32(ComboBoxGroup.SelectedValue.ToString()) }); } } CheckedUnits.DataSource = NewUnits; CheckedUnits.DisplayMember = "Name"; CheckedUnits.ValueMember = "UnitId"; using (DbOption db = new DbOption()) { if (NewUnits.Count != 0) { foreach (Units a in NewUnits) { try { db.Units.Add(new Units { Name = a.Name, UnitId = a.UnitId, GroupId = (int)ComboBoxGroup.SelectedValue }); db.SaveChanges(); } catch { MessageBox.Show("Не удалось добавить технику " + a.Name); } } } else { MessageBox.Show("Вы не выбрали технику"); } } } }
private void UserDeleteBtn_Click(object sender, EventArgs e) { using (DbOption db = new DbOption()) { int a = Convert.ToInt32(UserComboBox.SelectedValue.ToString()); var NewOption = db.UserOptions.Where(x => x.Id == a).FirstOrDefault(); db.UserOptions.Remove(NewOption); db.SaveChanges(); UserComboBox.DataSource = db.UserOptions; UserComboBox.DisplayMember = "UserName"; UserComboBox.ValueMember = "Id"; } }
private void AddOk_Click(object sender, EventArgs e) { using (DbOption db = new DbOption()) { if (ComboGroup.Text != null) { db.Groups.Add(new Groups { Name = ComboGroup.Text }); try { db.SaveChanges(); } catch { System.Windows.Forms.MessageBox.Show("Введите другое название группы. Это не соответсвует формату данных"); } this.ComboGroup.DataSource = db.Groups.ToList(); this.ComboGroup.DisplayMember = "Name"; this.ComboGroup.ValueMember = "GroupId"; } } }
private void RemoveBtn_Click(object sender, EventArgs e) { using (DbOption db = new DbOption()) { if (ComboGroup.Text != null) { int a = Convert.ToInt32(ComboGroup.SelectedValue.ToString()); var group = db.Groups.OrderByDescending(u => u.Name == ComboGroup.Text & u.GroupId == a).First(); db.Groups.Remove(group); var units = db.Units.Where(x => x.GroupId == a); db.Units.RemoveRange(units); db.SaveChanges(); this.ComboGroup.DataSource = db.Groups.ToList(); this.ComboGroup.DisplayMember = "Name"; this.ComboGroup.ValueMember = "GroupId"; } } }