public Form4(SageBook sageBook) { InitializeComponent(); sg = sageBook; using (MyDbContext cnt = new MyDbContext()) { comboBox1.DataSource = cnt.Sages.ToList(); comboBox1.ValueMember = "Id"; comboBox1.DisplayMember = "Name"; comboBox2.DataSource = cnt.Books.ToList(); comboBox2.ValueMember = "Id"; comboBox2.DisplayMember = "Title"; } if (sg.SageId != -1) { comboBox1.SelectedIndex = comboBox1.Items.IndexOf((comboBox1.DataSource as List <Sage>).Find(x => x.Id == sg.SageId)); comboBox2.SelectedIndex = comboBox2.Items.IndexOf((comboBox2.DataSource as List <Book>).Find(x => x.Id == sg.BookId)); } }
private void button7_Click(object sender, EventArgs e) { if (dataGridView1.SelectedRows.Count < 1) { return; } int index = dataGridView3.SelectedRows[0].Index; int idSage = (int)dataGridView3["SageId", index].Value; int idBook = (int)dataGridView3["BookId", index].Value; SageBook sg = new SageBook() { BookId = idBook, SageId = idSage }; using (MyDbContext cnt = new MyDbContext()) { cnt.Sages.Load(); Form4 form = new Form4(sg); form.ShowDialog(); if (idSage == sg.SageId && idBook == sg.BookId) { return; } if (cnt.Sages.Find(sg.SageId).Books.Contains(cnt.Books.Find(sg.BookId))) { MessageBox.Show("This sage already is author of this book. Thy again."); return; } cnt.Sages.Find(idSage).Books.Remove(cnt.Books.Find(idBook)); cnt.Sages.Find(sg.SageId).Books.Add(cnt.Books.Find(sg.BookId)); cnt.SaveChanges(); } loadData(); }
private void button9_Click(object sender, EventArgs e) { using (MyDbContext cnt = new MyDbContext()) { SageBook sg = new SageBook() { SageId = -1 }; Form4 form = new Form4(sg); form.ShowDialog(); if (cnt.Sages.Find(sg.SageId).Books.Contains(cnt.Books.Find(sg.BookId))) { MessageBox.Show("This sage already is author of this book. Thy again."); return; } cnt.Sages.Find(sg.SageId).Books.Add(cnt.Books.Find(sg.BookId)); cnt.SaveChanges(); } loadData(); }
public Form4(SageBook sageBook) { InitializeComponent(); sg = sageBook; using (DataClasses1DataContext cnt = new DataClasses1DataContext()) { comboBox1.Items.AddRange(cnt.Sage.Select(x => new SageToString() { Sage = x }).ToArray()); comboBox2.Items.AddRange(cnt.Book.Select(x => new BookToString() { Book = x }).ToArray()); } if (sg.Sage != null) { comboBox1.SelectedIndex = comboBox1.FindString(sg.Sage.name); comboBox2.SelectedIndex = comboBox2.FindString(sg.Book.title); } }