private void dgv_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
int MaxRows = dgv.Rows.Count;
for (int i = 0; i < MaxRows-1; i++)
{
SqlDataAdapter da = new SqlDataAdapter("SELECT CAST(originalPrice * " + (1.00 + (float.Parse(txtMarkUp.Text) / 100.00)).ToString() + " * " + (1.00 + (float.Parse(dgv.Rows[i].Cells[4].Value.ToString()) / 100.00)).ToString() + " AS decimal (8,2)) AS sellingPrice FROM Warehouse WHERE barcode = '" + Convert.ToString(dgv.Rows[i].Cells[2].Value) + "'", conn);
DataTable dt = new DataTable();
da.Fill(dt);
DataGridViewComboBoxColumn sellingPrice = dgv.Columns["sellingPrice"] as DataGridViewComboBoxColumn;
sellingPrice.DataSource = dt;
sellingPrice.ValueMember = "sellingPrice";
sellingPrice.DisplayMember = "sellingPrice";
dgv.Rows[i].Cells[5].Value = dt.Rows[0].ItemArray.GetValue(0);
dgv.Rows[i].Cells[4].Value = dt.Rows[0].ItemArray.GetValue(2).ToString(); //percent of tax for that category of product
}
}
此代码运行良好,但仅适用于组合框中的一个值...我需要组合框不同行中的不同值。我怎样才能做到这一点? 例如,当第 2 列中的产品发生变化时,我需要它来更改第 5 列的值,这是带有销售价格的组合框。 任何帮助将不胜感激,我几乎没有解决方案。谢谢。
最佳答案
假设您想为组合框列按行绑定(bind)不同的值,我不确定您想要什么 在那种情况下,如果您可以明智地确定条件,请使用此
(dgv.Rows[i].Cells[5] as DataGridViewComboBoxCell).DataSource = dt;
(dgv.Rows[i].Cells[5] as DataGridViewComboBoxCell).ValueMember = "sellingPrice";
(dgv.Rows[i].Cells[5] as DataGridViewComboBoxCell).DisplayMember = "sellingPrice";
这将为该特定单元格而不是您当前正在执行的列设置数据源。
希望这对您有所帮助。
关于c# - DataGridView 中不同行的不同 ComboBox 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855946/