SUBD_LAB/SUBD_LABA/View/FormAlbums.cs

90 lines
2.0 KiB
C#

using Commentbase;
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 FormAlbums : Form
{
private Abstracts abstracts;
public FormAlbums(Abstracts abstracts)
{
InitializeComponent();
this.abstracts = abstracts;
}
private void LoadData()
{
List<Album> albums = abstracts.GetAlbums();
dataGridView1.Rows.Clear();
if (dataGridView1.Columns.Count == 0)
{
dataGridView1.Columns.Add("Id", "Id");
dataGridView1.Columns.Add("Title", "title");
dataGridView1.Columns.Add("Description", "description");
}
foreach (Album album in albums)
{
dataGridView1.Rows.Add(album.Id, album.Title, album.Description);
}
}
private void FormAlbums_Load(object sender, EventArgs e)
{
LoadData();
}
private void buttonCreate_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAlbum));
if (service is FormAlbum form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void buttonReload_Click(object sender, EventArgs e)
{
LoadData();
}
private void buttonUpdate_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
var service = Program.ServiceProvider?.GetService(typeof(FormAlbum));
if (service is FormAlbum form)
{
form.AlbumId = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
var i = dataGridView1.CurrentCell.RowIndex;
var elem = (int)dataGridView1.Rows[i].Cells["Id"].Value;
abstracts.DeleteAlbum(elem);
LoadData();
}
}
}
}