0

I have DataGridView Control in windows application.
I load data and bind it to grid using above 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;

        }

where dgPO is DataGrdiView(DGV).
Now i want to update records in DGV when user leaves the row.
for it i have used RowLeave event of DGV.code is here

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();                   
                    }
}

When data is updated in DB, I refresh the DGV by calling LoadData method.

At this point, i get an error message like this.

"Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function"

I tried for available solution on SO and MSDN blogs,but they don't work for me like these

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

Help needed.
Thanks in advance !!

Community
  • 1
  • 1
ravidev
  • 2,708
  • 6
  • 26
  • 42

2 Answers2

0

Answer:

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

Suggestion: The alternative way

Did you try to use .RowValidating Event and .IsCurrentRowDirty Method?

   private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {
          if (dataGridView1.IsCurrentRowDirty)
          {
            //Your code here
          }
    }
spajce
  • 7,044
  • 5
  • 29
  • 44
0

use the following check before applying your edit in the "RowLeave" or "EndEdit" method:

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

This should work with any cell being edited.(the edit-code is not applied when losing focus; Typing Enter would suffice to apply the edit)

Apex ND
  • 440
  • 5
  • 12