DataGridView中不同行中的不同ComboBox值 [英] Different ComboBox values in DataGridView in different rows

查看:81
本文介绍了DataGridView中不同行中的不同ComboBox值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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列的值,即带有销售价格的组合框。
任何帮助将不胜感激,我几乎没有解决办法。谢谢。

This code is working perfectly but only for one values in combobox... I need different values in different rows for the combobox. How can I do that? In example when in column 2 product is changed, I need it to change the values of column 5 which is combobox with the selling prices. Any help would be appreciated, I have almost no solution for this in my mind. Thanks.

推荐答案

我不确定要为组合框列$ b逐行绑定其他值的假设。 $ b在这种情况下,如果可以确定条件行的明智之选,请使用此

I am not sure what you want assuming that you want to bind different values row wise for the combobox column In that Case if you can determine the condition row wise use this

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

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

This will set the DataSource for that particular cell instead of the Column which you are currently doing.

希望这会有所帮助。

这篇关于DataGridView中不同行中的不同ComboBox值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆