всё работает

This commit is contained in:
malimova 2024-05-08 14:11:00 +04:00
parent 66960902c9
commit 8244f9dd8d
3 changed files with 106 additions and 16 deletions

View File

@ -29,8 +29,7 @@ namespace LabWork
{ {
var view = _logic.ReadElement(new ContactSearchModel var view = _logic.ReadElement(new ContactSearchModel
{ {
Id = Id = _id.Value
_id.Value
}); });
if (view != null) if (view != null)
{ {

View File

@ -28,22 +28,23 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
dataGridView1 = new DataGridView(); dataGridView = new DataGridView();
buttonAdd = new Button(); buttonAdd = new Button();
buttonUpd = new Button(); buttonUpd = new Button();
buttonRef = new Button(); buttonRef = new Button();
buttonDel = new Button(); buttonDel = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
// dataGridView1 // dataGridView
// //
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.BackgroundColor = Color.LavenderBlush;
dataGridView1.Location = new Point(12, 12); dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Name = "dataGridView1"; dataGridView.Location = new Point(12, 12);
dataGridView1.RowHeadersWidth = 62; dataGridView.Name = "dataGridView";
dataGridView1.Size = new Size(717, 426); dataGridView.RowHeadersWidth = 62;
dataGridView1.TabIndex = 0; dataGridView.Size = new Size(717, 426);
dataGridView.TabIndex = 0;
// //
// buttonAdd // buttonAdd
// //
@ -63,6 +64,7 @@
buttonUpd.TabIndex = 2; buttonUpd.TabIndex = 2;
buttonUpd.Text = "Изменить"; buttonUpd.Text = "Изменить";
buttonUpd.UseVisualStyleBackColor = true; buttonUpd.UseVisualStyleBackColor = true;
buttonUpd.Click += buttonUpd_Click;
// //
// buttonRef // buttonRef
// //
@ -72,6 +74,7 @@
buttonRef.TabIndex = 3; buttonRef.TabIndex = 3;
buttonRef.Text = "Обновить"; buttonRef.Text = "Обновить";
buttonRef.UseVisualStyleBackColor = true; buttonRef.UseVisualStyleBackColor = true;
buttonRef.Click += buttonRef_Click;
// //
// buttonDel // buttonDel
// //
@ -81,6 +84,7 @@
buttonDel.TabIndex = 4; buttonDel.TabIndex = 4;
buttonDel.Text = "Удалить"; buttonDel.Text = "Удалить";
buttonDel.UseVisualStyleBackColor = true; buttonDel.UseVisualStyleBackColor = true;
buttonDel.Click += buttonDel_Click;
// //
// FormContacts // FormContacts
// //
@ -91,16 +95,17 @@
Controls.Add(buttonRef); Controls.Add(buttonRef);
Controls.Add(buttonUpd); Controls.Add(buttonUpd);
Controls.Add(buttonAdd); Controls.Add(buttonAdd);
Controls.Add(dataGridView1); Controls.Add(dataGridView);
Name = "FormContacts"; Name = "FormContacts";
Text = "Менеджер контактов"; Text = "Менеджер контактов";
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); Load += FormContacts_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
#endregion #endregion
private DataGridView dataGridView1; private DataGridView dataGridView;
private Button buttonAdd; private Button buttonAdd;
private Button buttonUpd; private Button buttonUpd;
private Button buttonRef; private Button buttonRef;

View File

@ -7,19 +7,105 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using LabWork.Interfaces;
namespace LabWork namespace LabWork
{ {
public partial class FormContacts : Form public partial class FormContacts : Form
{ {
public FormContacts() private readonly IContactLogic _logic;
public FormContacts(IContactLogic logic)
{ {
InitializeComponent(); InitializeComponent();
_logic = logic;
}
private void FormContacts_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["ContactName"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
} }
private void buttonAdd_Click(object sender, EventArgs e) private void buttonAdd_Click(object sender, EventArgs e)
{ {
var service = Program.ServiceProvider?.GetService(typeof(FormContact));
if (service is FormContact form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void buttonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service =
Program.ServiceProvider?.GetService(typeof(FormContact));
if (service is FormContact form)
{
form.Id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void buttonRef_Click(object sender, EventArgs e)
{
LoadData();
}
private void buttonDel_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);
try
{
if (!_logic.Delete(new ContactBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
} }
}
} }