title: RowLeave Event - Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function
body: <p>I have DataGridView Control in windows application.<br>
  I load data and bind it to grid using above code.</p>
  
  <pre><code>        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;
  
          }
  </code></pre>
  
  <p>where dgPO is DataGrdiView(DGV).<br>
  Now i want to update records in DGV when user leaves the row.<br>
  for it i have used RowLeave event of DGV.code is here  </p>
  
  <pre><code>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 &gt; 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();                   
                      }
  }
  </code></pre>
  
  <p>When data is updated in DB, I refresh the DGV by calling LoadData method.</p>
  
  <p>At this point, i get an error message like this.</p>
  
  <p><strong>"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"</strong></p>
  
  <p>I tried for available solution on SO and MSDN blogs,but they don't work for me
  like these</p>
  
  <p>1) <a href="http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/f824fbbf-9d08-4191-98d6-14903801acfc" rel="nofollow noreferrer">http://social.msdn.microsoft.com/Forums/en/winformsdatacontrols/thread/f824fbbf-9d08-4191-98d6-14903801acfc</a><br>
  2) <a href="https://stackoverflow.com/questions/7968916/click-on-any-other-cell-while-a-cell-of-same-row-of-datagridvew-is-in-edit-mode">Click on any other cell while a cell of same row of datagridvew is in edit mode causes Operation is not valid reentrant call</a><br>
  3) <a href="https://stackoverflow.com/questions/893752/invalidoperationexception-when-ending-editing-a-cell-moving-to-another-cell">InvalidOperationException - When ending editing a cell &amp; moving to another cell</a>  </p>
  
  <p>Help needed.<br>
  Thanks in advance !!</p>