dataGridView_CellContentClick:无法在控件上调用Invoke或BeginInvoke

时间:2011-03-01 02:22:52

标签: c#

任何人都可以告诉我这段代码我做错了什么,它产生了msg

“在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke。”

private void dataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

                if (e.ColumnIndex == buyColumn.Index)
                {

                    double openprice = (double)dataGridView.Rows[e.RowIndex].Cells[offerColumn.Index].Value;
                    string symbol = dataGridView.Rows[e.RowIndex].Cells[symbolColumn.Index].Value.ToString();
                    double quanity= (double)dataGridView.Rows[e.RowIndex].Cells[quanityColumn.Index].Value;

                    MessageBox.Show("I buy " + symbol +  " with "  + quanity + " at " + openprice);
            }        

更新:调试后,这3行导致问题,plz告诉我如何修复

double openprice = (double)dataGridView.Rows[e.RowIndex].Cells[offerColumn.Index].Value;
                        string symbol = dataGridView.Rows[e.RowIndex].Cells[symbolColumn.Index].Value.ToString();
                        double quanity= (double)dataGridView.Rows[e.RowIndex].Cells[quanityColumn.Index].Value;

1 个答案:

答案 0 :(得分:0)

尝试

        if (dataGridView.IsHandleCreated && e.ColumnIndex == buyColumn.Index)
        {

            double openprice = (double)dataGridView.Rows[e.RowIndex].Cells[offerColumn.Index].Value;
            string symbol = dataGridView.Rows[e.RowIndex].Cells[symbolColumn.Index].Value.ToString();
            double quanity= (double)dataGridView.Rows[e.RowIndex].Cells[quanityColumn.Index].Value;

            MessageBox.Show("I buy " + symbol +  " with "  + quanity + " at " + openprice);
         }