I have a tough one for today. I am creating an app in Windows Forms using C#, and I am supposed to make an app that shows every order with its status, date and full price. That is all taken care of. The second form is meant to be order details, where I am supposed to have OrderID, ProductName inside of a combobox, UnitPrice and Quantity columns. I have managed to create all of them, set their values through SQL using Dapper, but I am having a hard time getting the combobox to display the value of ProductID.
To simplify: I want the combobox to show the name of the product with the ProductID that is matching to the one given in the order details. If I change the Product inside of combobox, I need it to take the combobox ProductID value and change it in Order Details. If the order is a new one with no details, it should set it by default for the ProductID to be 1.
The combobox is inside of a DataGridView that is using a list of order details created by Dapper. Everything else works like a charm, I am just having a lot of trouble with the combobox.
Here is some code, I will add more if needed:
This is the code for the combobox itself
private void fillcombobox()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.HeaderText = "ProductName";
DataAccess dt = new DataAccess();
products = dt.GetAllProducts();
combo.DataSource = products;
combo.DisplayMember = "ProductName";
combo.ValueMember = "ProductID";
orderdetailsGridView.Columns.Add(combo);
combo.DisplayIndex = 0;
}
Here is the OrderDetails class
public class OrderDetails
{
public int OrderID { get; set; }
public int ProductID { get; set; }
public decimal UnitPrice { get; set; }
public int Quantity { get; set; }
}
Here is the Product class
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public decimal UnitPrice { get; set; }
public int UnitsInStock { get; set; }
}
Here is the query for the GetAllProducts
public List<Product> GetAllProducts()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("ShopDB")))
{
var output = connection.Query<Product>("select * from Products").ToList();
return output;
}
}