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

        }

Recommended Answers

All 2 Replies

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

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);
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.