繁体   English   中英

不同行中DataGridView中的不同ComboBox值

[英]Different ComboBox values in DataGridView in different rows

    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
        }
    }

此代码运行良好,但仅适用于 combobox 中的一个值...我需要 combobox 的不同行中的不同值。 我怎样才能做到这一点? 例如,当第 2 列中的产品发生更改时,我需要它来更改第 5 列的值,即 combobox 和售价。 任何帮助将不胜感激,我对此几乎没有解决方案。 谢谢。

我不确定您想要什么,假设您想为 combobox 列逐行绑定不同的值在这种情况下,如果您可以逐行确定条件,请使用此

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

这将为该特定单元格设置数据源,而不是您当前正在执行的列。

希望这可以帮助。

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

暂无
暂无

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

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