Первая форма готова.

This commit is contained in:
ElEgEv 2023-10-24 00:00:25 +04:00
parent 67698429f2
commit af7eb58283
4 changed files with 135 additions and 32 deletions

View File

@ -7,11 +7,11 @@ using System.Threading.Tasks;
namespace Contracts.ViewModel
{
public class DisciplineViewModel
public class DisciplineViewModel : IDiscipline
{
public int Id { get; set; }
public string Name { get; set; }
public string Name { get; set; } = string.Empty;
public DisciplineViewModel() { }

View File

@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Contracts.ViewModel
{
public class LabWorkViewModel
public class LabWorkViewModel : ILabWork
{
public int Id { get; set; }

View File

@ -29,8 +29,6 @@
private void InitializeComponent()
{
dataGridView = new DataGridView();
textBoxDiscipline = new TextBox();
label1 = new Label();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
@ -40,44 +38,26 @@
dataGridView.Location = new Point(12, 12);
dataGridView.Name = "dataGridView";
dataGridView.RowTemplate.Height = 25;
dataGridView.Size = new Size(234, 225);
dataGridView.Size = new Size(240, 225);
dataGridView.TabIndex = 0;
dataGridView.CellValueChanged += DataGridView_CellValueChanged;
dataGridView.KeyDown += DataGridView_KeyDown;
//
// textBoxDiscipline
//
textBoxDiscipline.Location = new Point(266, 30);
textBoxDiscipline.Name = "textBoxDiscipline";
textBoxDiscipline.Size = new Size(100, 23);
textBoxDiscipline.TabIndex = 1;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(252, 12);
label1.Name = "label1";
label1.Size = new Size(132, 15);
label1.TabIndex = 2;
label1.Text = "Название дисциплины";
//
// CreateDiscipline
// FormCreateDiscipline
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(391, 249);
Controls.Add(label1);
Controls.Add(textBoxDiscipline);
ClientSize = new Size(264, 249);
Controls.Add(dataGridView);
Name = "CreateDiscipline";
Name = "FormCreateDiscipline";
Text = "Создание дисциплин";
Load += FormCreateDiscipline_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
PerformLayout();
}
#endregion
private DataGridView dataGridView;
private TextBox textBoxDiscipline;
private Label label1;
}
}

View File

@ -1,4 +1,7 @@
using System;
using Contracts.BindingModel;
using Contracts.SearchModel;
using Contracts.StorageContracts;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,9 +15,129 @@ namespace VisualComponentsForm
{
public partial class FormCreateDiscipline : Form
{
public FormCreateDiscipline()
public readonly IDisciplineStorage _disciplineStorage;
public FormCreateDiscipline(IDisciplineStorage disciplineStorage)
{
InitializeComponent();
_disciplineStorage = disciplineStorage;
}
private void FormCreateDiscipline_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _disciplineStorage.GetFullList();
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
dataGridView.KeyDown += DataGridView_KeyDown;
dataGridView.CellValueChanged += DataGridView_CellValueChanged;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//обработчик нажатия клавиш
private void DataGridView_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert)
{
// Добавление новой строки при нажатии на клавишу Insert
var list = _disciplineStorage.GetFullList();
list.Add(new());
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
e.Handled = true;
}
else if (e.KeyCode == Keys.Delete)
{
// Удаление выбранных строк при нажатии на клавишу Delete
if (dataGridView.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Вы уверены, что хотите удалить выбранные записи?", "Подтверждение удаления", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
foreach (DataGridViewRow row in dataGridView.SelectedRows)
{
if (!row.IsNewRow)
{
int id = Convert.ToInt32(row.Cells["Id"].Value);
var view = _disciplineStorage.GetElement(new DisciplineSearchModel
{
Id = id
});
_disciplineStorage.Delete(new(view!));
}
}
LoadData();
}
}
e.Handled = true;
}
}
private void DataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
// Сохранение изменений при завершении редактирования ячейки
if (e.RowIndex >= 0)
{
DataGridViewRow row = dataGridView.Rows[e.RowIndex];
int id = Convert.ToInt32(row.Cells["Id"].Value);
string? name = row.Cells["Name"].Value?.ToString();
if (string.IsNullOrWhiteSpace(name))
{
// Запрещаем сохранение пустого имени
MessageBox.Show("Нельзя сохранить запись с пустым именем!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
LoadData();
}
else
{
var model = new DisciplineBindingModel
{
Id = id,
Name = name
};
if (model.Id == 0)
{
_disciplineStorage.Insert(model);
}
else
{
_disciplineStorage.Update(model);
}
LoadData();
}
}
}
}
}