SUBD_LAB/SUBD_LABA/View/FormAuthors.cs

107 lines
2.3 KiB
C#
Raw Permalink 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 FormAuthors : Form
{
private Abstracts db;
public FormAuthors(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(FormAuthor));
if (service is FormAuthor form)
{
form.AuthorId = 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.DeleteAuthor(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(FormAuthor));
if (service is FormAuthor 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("PhoneNum", "PhoneNum");
dataGridView1.Columns.Add("Email", "Email");
}
var list = db.GetAuthors();
if (list != null)
{
dataGridView1.Rows.Clear();
foreach (var author in list)
{
dataGridView1.Rows.Add(author.Id, author.Name, author.PhoneNum, author.Email);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormAuthors_Load(object sender, EventArgs e)
{
LoadData();
}
}
}