示例#1
0
        private void addToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            PlayerForm plForm = new PlayerForm();// создаем форму для города
            // из команд в бд формируем список

            int key = (int)dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value;

            plForm.comboBox1.DataSource = db.Countries.ToList();
            DialogResult result = plForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
                return;
            }

            City    city = new City();
            Country c    = (Country)plForm.comboBox1.SelectedItem;

            city.CountryId = c.Id;
            city.Name      = plForm.textBox1.Text;
            city.Country   = c;

            db.Cities.Add(city);
            db.SaveChanges();
            db.Countries.Load();
            dataGridView2.DataSource = db.Cities.Local.Where(p => p.CountryId == key).ToList();
        }
示例#2
0
        private void updToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                int index = dataGridView2.SelectedRows[0].Index;
                int id    = 0;
                int key   = (int)dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value;

                bool converted = Int32.TryParse(dataGridView2[0, index].Value.ToString(), out id);
                if (converted == false)
                {
                    return;
                }

                City       city   = db.Cities.Find(id);
                PlayerForm tmForm = new PlayerForm();
                tmForm.comboBox1.DataSource = db.Countries.ToList();
                tmForm.textBox1.Text        = city.Name;

                DialogResult result = tmForm.ShowDialog(this);
                if (result == DialogResult.Cancel)
                {
                    return;
                }

                city.Country         = (Country)tmForm.comboBox1.SelectedItem;
                city.Name            = tmForm.textBox1.Text;
                db.Entry(city).State = EntityState.Modified;
                db.SaveChanges();
                MessageBox.Show("Объект обновлен");
                db.Countries.Load();
                dataGridView2.DataSource = db.Cities.Local.Where(p => p.CountryId == key).ToList();
            }
        }