0

i have a datagridview and a button on my form. when values are entered into the 2 cells of the
datagridview and the button is pressed, the numbers are multiplied and the result is displayed in the third cell. this works alright. i now want the multiplication to be done automaticaly without pressing the button. that is when the value of the cell change it should call the function responsible for the calculation.
i tryed this but it keeps genetrating the error
An unhandled exception of the type 'System.StackOverflowException' occurred in System.Data.dll
please help me out
the codes

public void multiplyrows()
        {
            for (int i = 0; i < dgvSales.Rows.Count - 1; i++)
            {
                double a = Convert.ToDouble(dgvSales.Rows[i].Cells["Quantity Sold"].Value);
                double b = Convert.ToDouble(dgvSales.Rows[i].Cells["Unit Price"].Value);

                this.dgvSales.Rows[i].Cells[5].Value = (a*b).ToString("0.00");
            }
        }


private void dgvSales_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            multiplyrows();

        }
3
Contributors
2
Replies
21
Views
3 Years
Discussion Span
Last Post by ChrisHunter
0

CellValueChanged is called over and over until you get a stackoverflow. Every call to multiplyrows generates a CellvalueChanged event, which calls multiplyrows, which . . .

0

You could create a class like the one below and bind that to the DataGridView.

public class Calculate
{    
    public Calculate()
    {
       this.Clear();
    }

    public float Number1 { Get; Set; }

    public flost Number2 { Get; Set; }

    public float Sum 
    {
        get
        {
            return (Number1 + Number2);
        }
    }
}

Edited by ChrisHunter: Forgot the constructor

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.