RowLeave事件 - 操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用

时间:2013-02-15 06:19:41

标签: winforms datagridview invalidoperationexception

我在Windows应用程序中有DataGridView控件 我使用上面的代码加载数据并将其绑定到网格。

        private void LoadData()
        {
            clsData objData = new clsData();
            DataTable dtTemp = new System.Data.DataTable();
            dtTemp = objData.GetDatatable(" SELECT [ID],[CustomerName],[OrderQty],[Price],[POStatus],[Remarks],[EstdShipDate],[ActualShipDate],[IsShipped]  FROM [tblPO] ");
            dgPO.DataSource = null;
            dgPO.DataSource = dtTemp;

        }

其中dgPO是DataGrdiView(DGV) 现在我想在用户离开行时更新DGV中的记录 为此,我使用了DGV.code的RowLeave事件

private void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    int POId = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[0].Value) == "" ? 0 : Convert.ToInt32(dgPO.Rows[e.RowIndex].Cells[0].Value);
                    string CustName = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[1].Value);
                    int Qty = Convert.ToInt32(dgPO.Rows[e.RowIndex].Cells[2].Value);
                    decimal Price = Convert.ToDecimal(dgPO.Rows[e.RowIndex].Cells[3].Value);
                    string Status = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[4].Value);
                    string Remarks = Convert.ToString(dgPO.Rows[e.RowIndex].Cells[5].Value);
                    string dtEsdt = Convert.ToDateTime(dgPO.Rows[e.RowIndex].Cells[6].Value).ToString("yyyy-MM-dd");
                    string dtActl = Convert.ToDateTime(dgPO.Rows[e.RowIndex].Cells[7].Value).ToString("yyyy-MM-dd");
                    bool Shipped = dgPO.Rows[e.RowIndex].Cells[8].Value == DBNull.Value ? false : Convert.ToBoolean(dgPO.Rows[e.RowIndex].Cells[8].Value);

                    string strQry = "";
                    if (POId > 0)
                    {
                        strQry = " UPDATE [tblPO] SET [CustomerName] = '" + PreString(CustName) + "',[OrderQty] = " + Qty + ",[Price] = " + Price + ",[POStatus] = '" + Status + "'";
                        strQry = strQry + ",[Remarks] = '" + PreString(Remarks) + "',[EstdShipDate] = '" + dtEsdt + "',[ActualShipDate] = '" + dtActl + "',[IsShipped] = '" + Shipped + "'";
                        strQry = strQry + " WHERE ID =" + POId;
                    }
                    else
                    {
                        strQry = " INSERT INTO [tblPO] ([CustomerName],[OrderQty],[Price],[POStatus],[Remarks],[EstdShipDate],[ActualShipDate],[IsShipped]) ";
                        strQry += " VALUES('" + PreString(CustName) + "'," + Qty + "," + Price + ",'" + PreString(Status) + "','" + PreString(Remarks) + "'";
                        strQry += " ,'" + PreString(dtEsdt) + "','" + PreString(dtActl) + "','" + Shipped + "')";
                    }

                    clsData objData = new clsData();
                    if (objData.ExecuteQuery(strQry))
                    {
                        LoadData();                   
                    }
}

在DB中更新数据时,我通过调用LoadData方法刷新DGV。

此时,我收到这样的错误消息。

“操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用”

我尝试在SO和MSDN博客上提供可用的解决方案,但它们对我不起作用 喜欢这些

1)http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/f824fbbf-9d08-4191-98d6-14903801acfc
2)Click on any other cell while a cell of same row of datagridvew is in edit mode causes Operation is not valid reentrant call
3)InvalidOperationException - When ending editing a cell & moving to another cell

需要帮助。
在此先感谢!!

2 个答案:

答案 0 :(得分:0)

<强>答案:

    private void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        dataGridView1.EndEdit();
        //You code below
    }

建议:替代方式

您是否尝试使用.RowValidating Event.IsCurrentRowDirty Method

   private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
          if (dataGridView1.IsCurrentRowDirty)
          {
            //Your code here
          }
    }

答案 1 :(得分:0)

在“RowLeave”或“EndEdit”方法中应用编辑之前使用以下检查:

public void dgPO_RowLeave(object sender, DataGridViewCellEventArgs e) 
{
  if (!dgPO.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected)
                return;
  //...rest of your code to apply edit below...
}

这适用于正在编辑的任何单元格。(失去焦点时不应用编辑代码;输入Enter就足以应用编辑)