简体   繁体   English

C#根据gridview组合框更改gridview列数据?

[英]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. 我试图更改datagridview列中单元格的值,具体取决于同一datagridview中添加的组合框列中的选择。

I have twiked around with all sorts of BindingSources, but nothing seems to get it quite right. 我已经了解了各种各样的BindingSources,但是似乎没有什么是正确的。

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." 组合框从数据集“ Products”中的表中获取数据,而其余的数据网格则从“ 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. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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