Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 237
dex Avatar asked May 02 '11 10:05

dex


1 Answers

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.

Hope this helps.

like image 172
V4Vendetta Avatar answered Nov 17 '22 19:11

V4Vendetta