首页 文章

DataGridView中不同行中的不同ComboBox值

提问于
浏览
5
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的值改为销售价格 . 任何帮助将不胜感激,我在脑海中几乎没有解决方案 . 谢谢 .

2 回答

  • 0

    我不知道你想要什么,假设你想为rowobox列行明智地绑定不同的值在那种情况下你可以确定条件行明智地使用这个

    (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";
    

    这将为该特定单元格而不是您当前正在执行的列设置DataSource .

    希望这可以帮助 .

  • 7

    我认为你应该处理datagridview的CellParsing事件,当产品单元格被更改并且用户导航到另一个单元格时,用你想要的结果填充组合框 .

相关问题