Compare commits

..

2 Commits

Author SHA1 Message Date
4234c0c2da да 2024-05-08 14:11:26 +04:00
8244f9dd8d всё работает 2024-05-08 14:11:00 +04:00
4 changed files with 107 additions and 17 deletions

View File

@ -11,7 +11,7 @@ namespace LabWork
public class ContactViewModel
{
public int Id { get; set; }
[DisplayName("Название компонента")]
[DisplayName("Название контакта")]
public string ContactName { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Number { get; set; }

View File

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

View File

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

View File

@ -7,19 +7,105 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabWork.Interfaces;
namespace LabWork
{
public partial class FormContacts : Form
{
public FormContacts()
private readonly IContactLogic _logic;
public FormContacts(IContactLogic logic)
{
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)
{
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);
}
}
}
}
}
}