我想要更改datagridview列中單元格的值,具體取決於同一個datagridview中添加的組合框列中的選擇。C#根據gridview組合框更改gridview列數據?
我有各種各樣的BindingSources twiked,但似乎沒有得到它很正確。
我正在使用的代碼是:
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;
只要你有一個直觀,可以很容易: 我想在「價格」列的單元格取決於選擇的改變「產品「組合框。 組合框從數據集的一個表中獲取數據,稱爲「產品」,而其餘的數據網格則從名爲「行」的表中獲取數據。
問題是,是我唯一的選擇刪除「價格」列並手動添加另一列將隨組合框更改嗎?如果不是,我還有什麼其他選擇?
__不調用'DataGridView'''GridView'或'DataGrid'!這是錯誤的和混亂的,因爲這些是不同的控制。總是用__right__的名字打電話給他們!是的,它需要更多__four__字母來輸入.. - 至於你的問題:如果DGV是數據綁定的,你需要修改單元格而不是數據源。 – TaW
我嘗試過,但更改DataSource更改了所有列,我只想要更改一列。 我認爲沒有辦法做到這一點,除了手動添加另一列? –