示例#1
0
        /// <summary>
        /// редактирование картины
        /// </summary>
        private void btnPicChange_Click(object sender, EventArgs e)
        {
            //если нет выделенной ячейки, то выходим
            if (dgvPictures.CurrentCell == null)
            {
                return;
            }
            //индекс выделенной ячейки
            var i = dgvPictures.CurrentCell.RowIndex;
            //создаем форму редактирования
            var f = new Pictures();

            //заплняем объект класса Picture данными из datagridview
            f.Picture.pId           = (int)dgvPictures.Rows[i].Cells["pId"].Value;
            f.Picture.pArtist       = (int)dgvPictures.Rows[i].Cells["aId"].Value;
            f.Picture.pName         = Convert.ToString(dgvPictures.Rows[i].Cells["pName"].Value);
            f.Picture.pGenre        = Convert.ToString(dgvPictures.Rows[i].Cells["pGenre"].Value);
            f.Picture.pDateOfCreate = Convert.ToDateTime(dgvPictures.Rows[i].Cells["pDateOfCreate"].Value);
            //показываем диалог с редактированием
            if (f.ShowDialog() == DialogResult.OK)
            {
                //если нажали кнопку ОК пишем SQL по изменению данных о картине
                var cmd = new OleDbCommand(@"update Pictures set pName=?, pArtist=?, pDateOfCreate=?, pGenre=? where pId=?")
                {
                    Connection  = Db.Connection,
                    CommandType = CommandType.Text
                };
                //запоняем параметры для изменения данных
                cmd.Parameters.AddWithValue(@"pName", f.Picture.pName);
                cmd.Parameters.AddWithValue(@"pArtist", f.Picture.pArtist);
                cmd.Parameters.AddWithValue(@"pDateOfCreate", f.Picture.pDateOfCreate);
                cmd.Parameters.AddWithValue(@"pGenre", f.Picture.pGenre);
                cmd.Parameters.AddWithValue(@"pId", f.Picture.pId);
                //выполняем запрос по изменению
                cmd.ExecuteNonQuery();
                //обновляем данные в datagridview
                LoadPictures();
            }
        }