组合框列错误?

时间:2014-02-25 14:18:08

标签: c# winforms datagridview

我在gridview中使用了combobox列从

获取数据源
 DataGridViewComboBoxCell comboBoxCell = new DataGridViewComboBoxCell();
                                         comboBoxCell.ReadOnly = false;
                                         comboBoxCell.DataSource = itemDetails;
                                         comboBoxCell.DisplayMember = "UnitNameArabic";
                                         comboBoxCell.ValueMember = "UnitID";
                                         dataGridView1.Rows[i].Cells[2] = comboBoxCell;

我使事件CellValueChanged获取另一个数据取决于组合框选择的值 我在图片中得到错误,然后组合框运行良好 为什么我得到这个错误?

enter image description here

 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {

        if (e.ColumnIndex == 2)
        {

           for (int i = 0; i < dataGridView1.RowCount-1 ; i++)
            {

                {

                    List<ComboUnit> ItemUnitsList = new List<ComboUnit>();
                    ComboUnit ItemUnitsObj = new ComboUnit();


                    string itemnumber = dataGridView1.Rows[i].Cells[0].Value.ToString();

                    long SelectedVal = Convert.ToInt32(dataGridView1.Rows[i].Cells[2].Value);

                    TechTouch.Methods o = new TechTouch.Methods();
                    List<db.Vitems> itemDetails = o.SearchItem(itemnumber, "29");
                    itemDetails = itemDetails.GroupBy(Pp => Pp.UnitId).Select(y => y.First()).ToList();


                    foreach (var item in itemDetails)
                    {
                        if (item.UnitId == SelectedVal)
                        {

                            dataGridView1.Rows[i].Cells[4].Value = Convert.ToDouble(item.SalePrice);
                            double w = Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value);
                            double a = Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value);
                            double b = Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value);
                            dataGridView1.Rows[i].Cells[6].Value = Convert.ToDouble(((w * a)));
                        }
                    }

                }


            };


       }

1 个答案:

答案 0 :(得分:0)

您应该验证您的main上方有 [STAThread] 属性。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new Form1());
}

有关STAThread的更多信息以及为什么需要它,请参阅HERE