Реализована логика формы списка клиентов

This commit is contained in:
Никита Потапов 2024-04-07 00:28:00 +04:00
parent 305ce116c7
commit fbfb6f133b
4 changed files with 110 additions and 12 deletions

View File

@ -30,6 +30,9 @@
{ {
dataGridView = new DataGridView(); dataGridView = new DataGridView();
buttonDelete = new Button(); buttonDelete = new Button();
Id = new DataGridViewTextBoxColumn();
ClientFIO = new DataGridViewTextBoxColumn();
Email = new DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -37,9 +40,11 @@
// //
dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { Id, ClientFIO, Email });
dataGridView.Location = new Point(1, 0); dataGridView.Location = new Point(1, 0);
dataGridView.MultiSelect = false; dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView"; dataGridView.Name = "dataGridView";
dataGridView.RowHeadersVisible = false;
dataGridView.RowHeadersWidth = 51; dataGridView.RowHeadersWidth = 51;
dataGridView.RowTemplate.Height = 29; dataGridView.RowTemplate.Height = 29;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
@ -55,6 +60,30 @@
buttonDelete.TabIndex = 1; buttonDelete.TabIndex = 1;
buttonDelete.Text = "Удалить"; buttonDelete.Text = "Удалить";
buttonDelete.UseVisualStyleBackColor = true; buttonDelete.UseVisualStyleBackColor = true;
buttonDelete.Click += buttonDelete_Click;
//
// Id
//
Id.AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
Id.HeaderText = "ID";
Id.MinimumWidth = 6;
Id.Name = "Id";
Id.Width = 53;
//
// ClientFIO
//
ClientFIO.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
ClientFIO.HeaderText = "ФИО";
ClientFIO.MinimumWidth = 6;
ClientFIO.Name = "ClientFIO";
//
// Email
//
Email.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
Email.FillWeight = 50F;
Email.HeaderText = "Email";
Email.MinimumWidth = 6;
Email.Name = "Email";
// //
// FormClients // FormClients
// //
@ -65,6 +94,7 @@
Controls.Add(dataGridView); Controls.Add(dataGridView);
Name = "FormClients"; Name = "FormClients";
Text = "Клиенты"; Text = "Клиенты";
Load += FormClients_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -73,5 +103,8 @@
private DataGridView dataGridView; private DataGridView dataGridView;
private Button buttonDelete; private Button buttonDelete;
private DataGridViewTextBoxColumn Id;
private DataGridViewTextBoxColumn ClientFIO;
private DataGridViewTextBoxColumn Email;
} }
} }

View File

@ -1,20 +1,69 @@
using System; using Microsoft.Extensions.Logging;
using System.Collections.Generic; using SecuritySystemContracts.BindingModels;
using System.ComponentModel; using SecuritySystemContracts.BusinessLogicsContracts;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SecuritySystemView.Client namespace SecuritySystemView.Client
{ {
public partial class FormClients : Form public partial class FormClients : Form
{ {
public FormClients() private readonly ILogger _logger;
private readonly IClientLogic _logic;
public FormClients(ILogger<FormClients> logger, IClientLogic logic)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger;
_logic = logic;
}
private void FormClients_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Загрузка клиентов");
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки клиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void buttonDelete_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
_logger.LogInformation("Удаление клиента");
try
{
if (!_logic.Delete(new ClientBindingModel { Id = id }))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка удаления клиента");
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
} }
} }
} }

View File

@ -117,4 +117,22 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="Id.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ClientFIO.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Email.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Id.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="ClientFIO.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Email.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root> </root>

View File

@ -26,8 +26,6 @@ namespace SecuritySystemView
if (list != null) if (list != null)
{ {
dataGridViewComponents.DataSource = list; dataGridViewComponents.DataSource = list;
dataGridViewComponents.Columns["Id"].Visible = false;
dataGridViewComponents.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
} }
_logger.LogInformation("Загрузка компонентов"); _logger.LogInformation("Загрузка компонентов");
} }