0

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?

Windows Form Window

Nimrod Yanai
  • 777
  • 2
  • 8
  • 30
  • __Do not__ call a `DataGridView`a `GridView` or a `DataGrid`!! This is wrong and confusing as those are different controls. Always call things by their __right__ name! Yes, it takes __four__ letters more to type.. - As for your question: if the DGVs are databound you need to modify not the Cell but the DataSource. – TaW Apr 29 '16 at 15:44
  • I tried that, but changing the DataSource changes it for ALL columns, and I just want the one column to change. I take it there is no way to do this except adding another column manually? – Nimrod Yanai Apr 29 '16 at 15:55

1 Answers1

1

I haven't used combo boxes in data grids before but it might be a good idea to see if you can have an event handler on the change of the combo box index, if you can do that then use that to update the data grid every time it changes. If it isn't possible then you could do it as a separate combo box and populate that with the products when the form loads, and then every time they select a product it updates the grid view to show the price and quantity. Also from a visual standard personally i would have the product name at the beginning, but that is my person preference. Hope this helped.

Brendon
  • 334
  • 1
  • 2
  • 14
  • I CAN catch the combobox change, but that won't do anything yet because so far the price column isn't connected to the combobox at all. Changing the combobox won't change anything if I don't find a way to bind the Price column to the combobox column. – Nimrod Yanai Apr 29 '16 at 15:32
  • When you catch the change then you should be able to run a query that sets the price based on the Product. You can, on change re-render the datagridview to have the new values. Because if as you say you're able to catch the combobox change then it should be easy to change it through that. From what i can tell that is the only way – Brendon Apr 30 '16 at 17:26