Пример #1
0
        private void Add_Click(object sender, EventArgs e)
        {
            using (ArtistContext db = new ArtistContext())
              {
            ArtistData ArtForm = new ArtistData();

            //формируется список альбомов
            List<Album> albums = db.Albums.ToList();

            DialogResult result = ArtForm.ShowDialog(this);

            if (result == DialogResult.Cancel)
              return;

            Artist artist = new Artist();
            artist.Name = ArtForm.textBox1.Text;
            artist.Country = ArtForm.comboBox1.SelectedItem.ToString();
            artist.BirthDate = ArtForm.dateTimePicker1.Value.Date;

            db.Artists.Add(artist);
            db.SaveChanges();

            db.Artists.Load();
            dataGridView1.DataSource = db.Artists.Local.ToBindingList();

            MessageBox.Show("Новый объект добавлен");
              }
        }
Пример #2
0
        private void Change_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
              {
            int index = dataGridView1.SelectedRows[0].Index;
            int id = 0;
            bool converted = Int32.TryParse(dataGridView1[0, index].Value.ToString(), out id);
            if (converted == false)
              return;

            using (ArtistContext db = new ArtistContext())
            {
              Artist artist = db.Artists.Find(id);

              if (artist.Locked)
              {
            MessageBox.Show("Объект редактируется другим пользователем!");
            return;
              }
              //блокирую и отправляю запрос на лок
              artist.Locked = true;
              db.Entry(artist).State = EntityState.Modified;
              db.SaveChanges();

              ArtistData artistdata = new ArtistData();

              artistdata.comboBox1.SelectedItem = artist.Country;
              artistdata.textBox1.Text = artist.Name;
              artistdata.dateTimePicker1.Value = artist.BirthDate;

              DialogResult result = artistdata.ShowDialog(this);

              if (result == DialogResult.Cancel)
              {
            artist.Locked = false;
            db.Entry(artist).State = EntityState.Modified;
            db.SaveChanges();
            db.Artists.Load();
            dataGridView1.DataSource = db.Artists.Local.ToBindingList();
            return;
              }

              artist.Name = artistdata.textBox1.Text;
              artist.Country = artistdata.comboBox1.SelectedItem.ToString();
              artist.BirthDate = artistdata.dateTimePicker1.Value.Date;
              artist.Locked = false;

              db.Entry(artist).State = EntityState.Modified;
              db.SaveChanges();

              db.Artists.Load();
              dataGridView1.DataSource = db.Artists.Local.ToBindingList();
              dataGridView1.Refresh(); // обновляем грид
              MessageBox.Show("Объект обновлен");
            }

              }
        }