SUBD/View/UsersForm.cs

108 lines
2.3 KiB
C#

using Database;
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 UsersForm : Form
{
private Abstracts db;
public UsersForm(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(UserForm));
if (service is UserForm form)
{
form.UserId = 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.DeleteUser(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(UserForm));
if (service is UserForm 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("Email", "Email");
dataGridView1.Columns.Add("Birthday", "Birthday");
}
var list = db.GetUsers();
if (list != null)
{
dataGridView1.Rows.Clear();
foreach (var user in list)
{
dataGridView1.Rows.Add(user.Id, user.name, user.email, user.birthday);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void UsersForm_Load(object sender, EventArgs e)
{
LoadData();
}
}
}