diff --git a/InternetShop/InternetShopForms/InternetShopForms.csproj b/InternetShop/InternetShopForms/InternetShopForms.csproj index a1bce66..28539aa 100644 --- a/InternetShop/InternetShopForms/InternetShopForms.csproj +++ b/InternetShop/InternetShopForms/InternetShopForms.csproj @@ -9,8 +9,8 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs b/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs index ad98c61..ed8cd4d 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs +++ b/InternetShop/InternetShopForms/Products/FormProductsList.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { dataGridView = new DataGridView(); + ColumnId = new DataGridViewTextBoxColumn(); + ColumnName = new DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); SuspendLayout(); // @@ -40,7 +42,9 @@ dataGridView.AllowUserToResizeRows = false; dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Columns.AddRange(new DataGridViewColumn[] { ColumnId, ColumnName }); dataGridView.Dock = DockStyle.Fill; + dataGridView.EditMode = DataGridViewEditMode.EditOnF2; dataGridView.Location = new Point(0, 0); dataGridView.MultiSelect = false; dataGridView.Name = "dataGridView"; @@ -52,6 +56,22 @@ dataGridView.CellEndEdit += dataGridView_CellEndEdit; dataGridView.CellValidating += dataGridView_CellValidating; // + // ColumnId + // + ColumnId.DataPropertyName = "Id"; + ColumnId.HeaderText = "Id"; + ColumnId.MinimumWidth = 6; + ColumnId.Name = "ColumnId"; + ColumnId.ReadOnly = true; + ColumnId.Visible = false; + // + // ColumnName + // + ColumnName.DataPropertyName = "Name"; + ColumnName.HeaderText = "Name"; + ColumnName.MinimumWidth = 6; + ColumnName.Name = "ColumnName"; + // // FormProductsList // AutoScaleDimensions = new SizeF(8F, 20F); @@ -69,5 +89,7 @@ #endregion private DataGridView dataGridView; + private DataGridViewTextBoxColumn ColumnId; + private DataGridViewTextBoxColumn ColumnName; } } \ No newline at end of file diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.cs b/InternetShop/InternetShopForms/Products/FormProductsList.cs index 3215cbe..08f9f2d 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.cs +++ b/InternetShop/InternetShopForms/Products/FormProductsList.cs @@ -8,7 +8,7 @@ namespace InternetShopForms.Products public partial class FormProductsList : Form { private IProductLogic _productLogic; - private List _products; + private List _products = new List(); public FormProductsList(IProductLogic productLogic) { InitializeComponent(); @@ -18,19 +18,6 @@ namespace InternetShopForms.Products private void FormProductsList_Load(object sender, EventArgs e) { - var idColumn = new DataGridViewColumn(); - var nameColumn = new DataGridViewColumn(); - var dataGridViewCell = new DataGridViewTextBoxCell(); - idColumn.Visible = false; - idColumn.DataPropertyName = "Id"; - idColumn.Name = "Id"; - idColumn.CellTemplate = dataGridViewCell; - nameColumn.DataPropertyName = "Name"; - nameColumn.HeaderText = "Название"; - nameColumn.Name = "Name"; - nameColumn.CellTemplate = dataGridViewCell; - dataGridView.Columns.Add(idColumn); - dataGridView.Columns.Add(nameColumn); LoadData(); } @@ -42,7 +29,7 @@ namespace InternetShopForms.Products dataGridView.Rows.Clear(); foreach (var product in _products) { - dataGridView.Rows.Add(product); + dataGridView.Rows.Add(product.Id, product.Name); } } catch (Exception ex) @@ -55,8 +42,15 @@ namespace InternetShopForms.Products { if (e.KeyCode == Keys.Insert) { - dataGridView.Rows.Add(); - dataGridView.CurrentCell = dataGridView.Rows[dataGridView.RowCount - 1].Cells[1]; + try + { + dataGridView.Rows.Add(); + dataGridView.CurrentCell = dataGridView.Rows[dataGridView.RowCount - 1].Cells[1]; + } + catch (Exception ex) + { + dataGridView.Rows.RemoveAt(dataGridView.RowCount - 1); + } e.Handled = true; } else if (e.KeyCode == Keys.Delete) @@ -64,16 +58,16 @@ namespace InternetShopForms.Products if (dataGridView.SelectedRows.Count == 1) { DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; - var selectedItemIdValue = selectedRow.Cells["Id"].Value; + var selectedItemIdValue = selectedRow.Cells["ColumnId"].Value; if (selectedItemIdValue == null) { dataGridView.Rows.Remove(selectedRow); return; } int selectedItemId = Convert.ToInt32(selectedItemIdValue); - string selectedItemName = (string)selectedRow.Cells["Name"].Value; + string selectedItemName = (string)selectedRow.Cells["ColumnName"].Value; var result = MessageBox.Show( - $"Удалить товар \"{selectedRow.Cells["Id"].Value}\"?", + $"Удалить товар \"{selectedRow.Cells["ColumnId"].Value}\"?", "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question @@ -100,19 +94,19 @@ namespace InternetShopForms.Products try { DataGridViewRow currentRow = dataGridView.Rows[e.RowIndex]; - if (currentRow.Cells["Id"].Value == null || Convert.ToInt32(currentRow.Cells["Id"].Value) == 0) + if (currentRow.Cells["ColumnId"].Value == null || Convert.ToInt32(currentRow.Cells["ColumnId"].Value) == 0) { _productLogic.Create(new ProductBindingModel { - Name = currentRow.Cells["Name"].Value.ToString()!, + Name = currentRow.Cells["ColumnName"].Value.ToString()!, }); } else { _productLogic.Update(new ProductBindingModel { - Id = Convert.ToInt32(currentRow.Cells["Id"].Value), - Name = currentRow.Cells["Name"].Value.ToString(), + Id = Convert.ToInt32(currentRow.Cells["ColumnId"].Value), + Name = currentRow.Cells["ColumnName"].Value.ToString()!, }); } MessageBox.Show("Запись сохранена"); diff --git a/InternetShop/InternetShopForms/Products/FormProductsList.resx b/InternetShop/InternetShopForms/Products/FormProductsList.resx index 8b2ff64..12eaa02 100644 --- a/InternetShop/InternetShopForms/Products/FormProductsList.resx +++ b/InternetShop/InternetShopForms/Products/FormProductsList.resx @@ -117,4 +117,10 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + \ No newline at end of file