Home > other >  Stackoverflow exception during during DGV cell value change Event triggers
Stackoverflow exception during during DGV cell value change Event triggers

Time:11-13

i am trying to multiply the value of two cells from DGV and saving it to another cell. but aim getting stackOverFlow Exception.

        private void ReturnCartDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            long totalPrice;
            int rowIndex = ReturnCartDGV.CurrentCell.RowIndex;
            totalPrice = (Convert.ToInt64(ReturnCartDGV.Rows[rowIndex].Cells[4].Value) * Convert.ToInt64(ReturnCartDGV.Rows[rowIndex].Cells[5].Value));
            this.ReturnCartDGV.Rows[rowIndex].Cells[6].Value = totalPrice.ToString();
            
        }

CodePudding user response:

You are changing the Value of a cell in the CellValueChanged event handler, which will raise the CellValueChanged event, which will continue on ad infinitum until the stack overflows. You are only interested in the Value changing in two specific columns, so you should be checking whether the event was raised by a cell in either of those two columns. If it wasn't then you don't do anything. The e parameter will tell you which column the cell was in whose Value changed.

  •  Tags:  
  • c#
  • Related