private void _editBtn_Click(object sender, EventArgs e) { if (DataGridView.Rows.Count == 0) { MessageBox.Show("Таблица пуста"); } else { //Get selected row. int index = DataGridView.CurrentRow.Index; InsertForm f = new InsertForm(); f.Owner = this; //Add data from DataGridView to InsertForm. f.Title = (string)DataGridView.Rows[index].Cells[0].Value; f.Author = (string)DataGridView.Rows[index].Cells[1].Value; f.Category = (string)DataGridView.Rows[index].Cells[2].Value; f.Price = (string)DataGridView.Rows[index].Cells[3].Value; f.Year = (string)DataGridView.Rows[index].Cells[4].Value; f.ShowDialog(); AddingData(f, index); } }
/// <summary> /// Adding and editing data in DataGridView /// </summary> /// <param name="f">InsertForm</param> /// <param name="index">edited row</param> private void AddingData(InsertForm f, int index) { DataGridView.AllowUserToAddRows = false; int n; if (index < 0) { n = DataGridView.Rows.Count; DataGridView.Rows.Add(); } else { int columnsCount = DataGridView.Columns.Count; n = index; for (int i = 0; i < columnsCount; i++) { DataGridView.Rows[n].Cells[i].Value = ""; } } DataGridView.Rows[n].Cells[0].Value = f.Title; DataGridView.Rows[n].Cells[1].Value = f.Author; DataGridView.Rows[n].Cells[2].Value = f.Category; if (!f.Price.Contains(".") && !(f.Price == "")) { DataGridView.Rows[n].Cells[3].Value = f.Price + ".00"; } else { DataGridView.Rows[n].Cells[3].Value = f.Price; } DataGridView.Rows[n].Cells[4].Value = f.Year; }
private void _AddBtn_Click(object sender, EventArgs e) { InsertForm f = new InsertForm(); //Link annex form to main one. f.Owner = this; f.ShowDialog(); //If user presses "Отмена", do nothing. if (f.ActiveControl.Text != "Отмена") { AddingData(f, -1); } }