SUBD_LAB/SUBD_LABA/View/FormLocations.cs

106 lines
2.3 KiB
C#
Raw Normal View History

2024-05-14 22:32:28 +04:00
using Commentbase;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace View
{
public partial class FormLocations : Form
{
private Abstracts db;
public FormLocations(Abstracts abstracts)
{
InitializeComponent();
db = abstracts;
}
private void buttonReload_Click(object sender, EventArgs e)
{
LoadData();
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
var service = Program.ServiceProvider?.GetService(typeof(FormLocation));
if (service is FormLocation form)
{
form.LocationId = id;
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
try
{
db.DeleteLocation(id);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
LoadData();
}
}
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormLocation));
if (service is FormLocation form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void LoadData()
{
try
{
if (dataGridView1.ColumnCount == 0)
{
dataGridView1.Columns.Add("Id", "Id");
dataGridView1.Columns.Add("Name", "Name");
dataGridView1.Columns.Add("ShortName", "Shortname");
}
var list = db.GetLocations();
if (list != null)
{
dataGridView1.Rows.Clear();
foreach (var location in list)
{
dataGridView1.Rows.Add(location.Id, location.Name, location.ShortName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormLocations_Load(object sender, EventArgs e)
{
LoadData();
}
}
}