I am trying to change the value of a cell in a datagridview column, depending on the selection in an added combobox column in the same datagridview.
I have twiked around with all sorts of BindingSources, but nothing seems to get it quite right.
The code I'm using is:
conn = new SqlConnection(DBConnectionString);
select_order = new SqlCommand("SELECT orderNum, orderBy, orderShipadrs, orderDate FROM tblOrders WHERE orderNum=" + OrderID, conn);
da1 = new SqlDataAdapter(select_order);
select_lines = new SqlCommand("SELECT linenum, fkprdctnum, lineQuantity, lineprdctPrice FROM tblOrderLines WHERE fkOrdernum=" + OrderID, conn);
da2 = new SqlDataAdapter(select_lines);
da2.MissingSchemaAction = MissingSchemaAction.AddWithKey;
select_products = new SqlCommand("SELECT * FROM tblProducts", conn);
da3 = new SqlDataAdapter(select_products);
Orders = new DataSet();
try
{
da1.Fill(Orders, Order);
da2.Fill(Orders, Lines);
da3.Fill(Orders, Products);
}
catch
{
MessageBox.Show("There was an error connecting to the database.");
}
DataRelation prdctprice = new DataRelation(Relations, Orders.Tables[Products].Columns["prdctNum"], Orders.Tables[Lines].Columns["fkPrdctnum"]);
Orders.Relations.Add(prdctprice);
CurrentOrder.DataSource = Orders.Tables[Order];
CurrentOrder.Columns["orderNum"].HeaderText = "Number";
CurrentOrder.Columns["orderNum"].Width = 50;
CurrentOrder.Columns["orderBy"].HeaderText = "Customer Name";
CurrentOrder.Columns["orderBy"].Width = 150;
CurrentOrder.Columns["orderShipadrs"].HeaderText = "Shipping Address";
CurrentOrder.Columns["orderShipadrs"].Width = 200;
CurrentOrder.Columns["orderDate"].HeaderText = "Order Date";
CurrentOrder.Columns["orderDate"].Width = 100;
//masterBindingSource.DataSource = Orders;
//masterBindingSource.DataMember = Products;
CurrentLine.DataSource = Orders.Tables["Lines"];
DataGridViewComboBoxColumn prod = new DataGridViewComboBoxColumn();
prod.DataSource = Orders.Tables[Products];
prod.DisplayMember = "prdctName";
prod.ValueMember = "prdctNum";
prod.HeaderText = "Product";
CurrentLine.Columns.Add(prod);
//detailBindingSource.DataSource = masterBindingSource;
//detailBindingSource.DataMember = Relations;
CurrentLine.Columns["fkPrdctnum"].Visible = false;
CurrentLine.Columns["linenum"].Width = 60;
CurrentLine.Columns["linenum"].HeaderText = "Number";
CurrentLine.Columns["linenum"].ReadOnly = true;
CurrentLine.Columns["lineQuantity"].HeaderText = "Quantity";
CurrentLine.Columns["lineQuantity"].Width = 70;
CurrentLine.Columns["linePrdctPrice"].HeaderText = "Price";
CurrentLine.Columns["linePrdctPrice"].ReadOnly = true;
Just so you have a visual to make it easy: I want the cells in the "Price" column to change depending on the selection in the "Product" combobox. The combobox is taking it's data from a table in the dataset called "Products," while the rest of the datagrid is taking it's data from a table called "Lines."
The question is, is my only option to remove the "Price" column and manually add another column that will change with the combobox? If not, what other options do I have?