// Add a new product + supplier combination private void btnAddProduct_Click(object sender, EventArgs e) { string selProd = cboProductList.Text; string selSup = cboSupplierList.Text; if (ProductSupplierDB.CheckProductSupplierExists(selProd, selSup)) // Combination exists in the database, warn the user { MessageBox.Show("Warning: Item already exists.", "Duplicate Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else // Combination is not in database, add product { ProductSupplierDB.AddProduct(selProd, selSup); dgvProducts.DataSource = ProductSupplierDB.GetProducts(); var MaxID = dgvProducts.Rows.Cast <DataGridViewRow>() .Max(r => Convert.ToInt32(r.Cells["ID"].Value)); int rowIndex = 0; foreach (DataGridViewRow row in dgvProducts.Rows) { if (row.Cells["ID"].Value.ToString() != MaxID.ToString()) { rowIndex++; } else { dgvProducts.Rows[rowIndex].Selected = true; dgvProducts.FirstDisplayedScrollingRowIndex = rowIndex; break; } } } }
// Triggers when the form loads private void frmProduct_Load(object sender, EventArgs e) { // Fills the dataviewgrid with table data pulled from the database dgvProducts.DataSource = ProductSupplierDB.GetProducts(); dgvProducts.Columns[0].Width = 42; dgvProducts.Columns[1].Width = 145; dgvProducts.Columns[2].Width = 316; // Data binding for the Products & Suppliers drop-down lists this.productsTableAdapter.Fill(this.travelExpertsDataSet.Products); // Load the suppliers combo box this.LoadSupplierComboBox(); }