using GasStation.Repositories; using GasStation.Entities; namespace GasStation.Forms { public partial class FormSupply : Form { private readonly ISupplyRepository _supplyRepository; public FormSupply(ISupplyRepository supplyRepository, ISupplierRepository supplierRepository, IProductRepository productRepository) { InitializeComponent(); _supplyRepository = supplyRepository ?? throw new ArgumentNullException(nameof(supplyRepository)); comboBoxSupplier.DataSource = supplierRepository.ReadSupplier(); comboBoxSupplier.DisplayMember = "SupplierName"; comboBoxSupplier.ValueMember = "ID"; ColumnProduct.DataSource = productRepository.ReadProduct(); ColumnProduct.DisplayMember = "Cost"; ColumnProduct.ValueMember = "ID"; } private void ButtonSave_Click(object sender, EventArgs e) { try { if (dataGridViewSupply.RowCount < 1 || comboBoxSupplier.SelectedIndex < 0) { throw new Exception("Имеются незаполненые поля"); } _supplyRepository.CreateSupply(Supply.CreateSupply(0, (int)comboBoxSupplier.SelectedValue!, 0, CreateListSellingFromDataGrid())); Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } private void ButtonCancel_Click(object sender, EventArgs e) => Close(); private List CreateListSellingFromDataGrid() { var list = new List(); foreach (DataGridViewRow row in dataGridViewSupply.Rows) { if (row.Cells["ColumnProduct"].Value == null || row.Cells["ColumnCount"].Value == null) { continue; } list.Add(SupplySupply.CreateSupply(0, Convert.ToInt32(row.Cells["ColumnCount"].Value))); } return list; } } }