private void btnChange_Click(object sender, EventArgs e) { if (dgvTariffs.SelectedRows.Count > 0) { int selectedIndex = dgvTariffs.SelectedRows[0].Index; int id = 0; bool converted = int.TryParse(dgvTariffs[0, selectedIndex].Value.ToString(), out id); if (!converted) { return; } Tariff tariff = db.Tariffs.Find(id); DFormTariff dFormTariff = new DFormTariff(); dFormTariff.tbName.Text = tariff.Name; dFormTariff.ntbPerHour.Value = (decimal)tariff.PerHour; dFormTariff.ntbPerKG.Value = (decimal)tariff.PerKG; dFormTariff.ntbPerKM.Value = (decimal)tariff.PerKM; dFormTariff.rtbDescription.Text = tariff.Description; DialogResult dialogResult = dFormTariff.ShowDialog(this); if (dialogResult == DialogResult.Cancel) { return; } tariff.Name = dFormTariff.tbName.Text; tariff.PerHour = (int)dFormTariff.ntbPerHour.Value; tariff.PerKG = (int)dFormTariff.ntbPerKG.Value; tariff.PerKM = (int)dFormTariff.ntbPerKM.Value; tariff.Description = dFormTariff.rtbDescription.Text; db.SaveChanges(); dgvTariffs.Refresh(); LoadDescription(this, EventArgs.Empty); MessageBox.Show("Объект обновлен.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void LoadDescription(object sender, EventArgs e) { if (dgvTariffs.SelectedRows.Count > 0) { int selectedIndex = dgvTariffs.SelectedRows[0].Index; int id = 0; bool converted = int.TryParse(dgvTariffs[0, selectedIndex].Value.ToString(), out id); if (!converted) { return; } Tariff tariff = db.Tariffs.Find(id); rtbDescription.Text = tariff.Description; } else { rtbDescription.Text = ""; } }
private void btnRemove_Click(object sender, EventArgs e) { if (dgvTariffs.SelectedRows.Count > 0) { int selectedIndex = dgvTariffs.SelectedRows[0].Index; int id = 0; bool converted = int.TryParse(dgvTariffs[0, selectedIndex].Value.ToString(), out id); if (!converted) { return; } Tariff tariff = db.Tariffs.Find(id); db.Tariffs.Remove(tariff); db.SaveChanges(); SearchObjects(this, EventArgs.Empty); MessageBox.Show("Объект удален.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); } }