简体   繁体   中英

C# change gridview column data according to gridview combobox?

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窗体窗口

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM