Good day, so I’ve been working on this project in one of our courses. It’s a sales and inventory system, and I am having problems with my DataGridView.
InvenToryForm
It doesn’t fill the space, even though its dock is already on fill. And I have two DataGridViewImageCellLayout which is Edit and Delete at the column and it doesn’t show up. It’s supposed to look like this.
TransactiomForm
Here’s my code in my DataGridView in InventoryForm
private void dgvInventory_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewRow row = dgvInventory.Rows[e.RowIndex];
string columnName = dgvInventory.Columns[e.ColumnIndex].Name;
if (columnName == "Edit")
{
ManageInventoryForm manageInventory = new ManageInventoryForm(this);
manageInventory.txtProductID.Text = row.Cells["product_id"].Value.ToString();
manageInventory.txtProductName.Text = row.Cells["product_name"].Value.ToString();
manageInventory.txtQuantity.Text = row.Cells["quantity"].Value.ToString();
manageInventory.txtStock.Text = row.Cells["minimum_stock"].Value.ToString();
manageInventory.txtPrice.Text = row.Cells["price"].Value.ToString();
manageInventory.EnableButtons(false, true);
manageInventory.ShowDialog();
}
else if (columnName == "Delete")
{
if (MessageBox.Show("Are you sure you want to delete this Product?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
// Delete from tbl_product
SqlCommand cmdProduct = new SqlCommand("DELETE FROM tbl_product WHERE product_id = @product_id", conn);
cmdProduct.Parameters.AddWithValue("@product_id", row.Cells["product_id"].Value);
cmdProduct.ExecuteNonQuery();
// Delete from tbl_stock
SqlCommand cmdStock = new SqlCommand("DELETE FROM tbl_stock WHERE product_id = @product_id", conn);
cmdStock.Parameters.AddWithValue("@product_id", row.Cells["product_id"].Value);
cmdStock.ExecuteNonQuery();
MessageBox.Show("Product and stock records have been successfully deleted!");
LoadInventory(); // Refresh the grid after deletion
}
}
}
}
}
}
I have no idea what’s wrong with my code, please help me out.
here’s also the code for my TransactionForm
private void dgvTransaction_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewRow row = dgvTransaction.Rows[e.RowIndex];
string columnName = dgvTransaction.Columns[e.ColumnIndex].Name;
if (columnName == "Edit")
{
TransactionForm manageTransaction = new TransactionForm();
manageTransaction.txtProductID.Text = row.Cells[1].Value.ToString();
manageTransaction.txtQuantity.Text = row.Cells[2].Value.ToString();
manageTransaction.txtTime.Text = row.Cells[4].Value.ToString();
manageTransaction.ShowDialog();
}
else if (columnName == "Delete")
{
if (MessageBox.Show("Are you sure you want to delete this transaction?", "Delete Record", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("DELETE FROM tbl_transactions WHERE transaction_id = @transaction_id", conn);
cmd.Parameters.AddWithValue("@transaction_id", row.Cells[0].Value);
cmd.ExecuteNonQuery();
MessageBox.Show("Transaction record has been successfully deleted!");
LoadTransaction(); // Refresh the grid after deletion
}
}
}
}
}
I tried adding the actual paths of the images for the edit and delete but still doesn’t work