コード例 #1
0
        private void Add_Click(object sender, EventArgs e)
        {
            AlbumData albumdata = new AlbumData();

              // формируем список исполнителей
              List<Artist> artists = db.Artists.ToList();
              albumdata.comboBox1.DataSource = artists;
              albumdata.comboBox1.ValueMember = "Id";
              albumdata.comboBox1.DisplayMember = "Name";

              // формируем список жанров
              List<Genre> genres = db.Genres.ToList();
              albumdata.comboBox2.DataSource = genres;
              albumdata.comboBox2.ValueMember = "Id";
              albumdata.comboBox2.DisplayMember = "Name";

              DialogResult result = albumdata.ShowDialog(this);

              if (result == DialogResult.Cancel)
            return;

              Album album = new Album();
              album.Name = albumdata.textBox2.Text;
              album.Year = albumdata.dateTimePicker1.Value.Date;
              album.Artist = (Artist)albumdata.comboBox1.SelectedItem;
              album.Genre = (Genre)albumdata.comboBox2.SelectedItem;

              db.Albums.Add(album);
              db.SaveChanges();

              db.Dispose();
              db = new ArtistContext();
              db.Albums.Load();
              dataGridView1.DataSource = db.Albums.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;

            db.Dispose();
            db = new ArtistContext();

            db.Albums.Load();
            Album album = db.Albums.Find(id);

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

            AlbumData albumdata = new AlbumData();
            albumdata.textBox2.Text = album.Name;
            albumdata.dateTimePicker1.Value = album.Year;

            // формируем список жанров
            List<Genre> genres = db.Genres.ToList();
            albumdata.comboBox2.DataSource = genres;
            albumdata.comboBox2.ValueMember = "Id";
            albumdata.comboBox2.DisplayMember = "Name";

            // формируем список исполнителей
            List<Artist> artists = db.Artists.ToList();
            albumdata.comboBox1.DataSource = artists;
            albumdata.comboBox1.ValueMember = "Id";
            albumdata.comboBox1.DisplayMember = "Name";

            if (album.Genre != null)
              albumdata.comboBox2.SelectedValue = album.Genre.Id;

            if (album.Artist != null)
              albumdata.comboBox1.SelectedValue = album.Artist.Id;

            DialogResult result = albumdata.ShowDialog(this);

            if (result == DialogResult.Cancel)
            {
              album.Locked = false;
              db.Entry(album).State = EntityState.Modified;
              db.SaveChanges();

              db.Albums.Load();
              dataGridView1.DataSource = db.Albums.Local.ToBindingList();
              return;
            }

            album.Name = albumdata.textBox2.Text;
            album.Year = albumdata.dateTimePicker1.Value.Date;
            album.Artist = (Artist)albumdata.comboBox1.SelectedItem;
            album.Genre = (Genre)albumdata.comboBox2.SelectedItem;
            album.Locked = false;

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

            db.Dispose();
            db = new ArtistContext();
            db.Albums.Load();
            dataGridView1.DataSource = db.Albums.Local.ToBindingList();

            MessageBox.Show("Объект обновлен");
              }
        }