Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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");
            }
        }


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

        }
Posted
Comments
idenizeni 15-Aug-13 11:51am    
The StackOverflowException is caused by your code, you are calling multiplyrows() from the CellValueChanged routine and then changing the values on the dgvSales in your multiplyrows(). Your code is 'stuck' in a loop here. Basically, everytime you enter the multiplerows() routine you trigger another call to the CellValueChanged routine which in turn calls the multiplerows() routine, this would repeat forever.

1) Have you set a breakpoint in your method to see if it even gets hit?
2) Your convert calls are very dangerous as you have them coded because they assume that every cell referenced will ALWAYS contain a valid double value. The safer way to do this would be like this:
C#
double a, b;
if( double.TryParse( dgvSales.Rows[i].Cells["Quantity Sold"].Value, out a ) &&
    double.TryParse( dgvSales.Rows[i].Cells["Unit Price"].Value, out b ) )
{
   this.dgvSales.Rows[i].Cells[5].Value = (a*b).ToString("0.00");
}
else
{
   this.dgvSales.Rows[i].Cells[5].Value = "Some default value";
}


[Edit]
As noted in the comments, this does not address the root issue at hand which is your infinite loop being created by updating cells directly. The way to solve this is to have an underlying DataTable which is used as a DataSource for the DataGridView control. In the cell value changed handler, update the underlying DataTable value and then perform your calculations on that object. When complete, simply rebind your DataGridView and you should be ok. But please use the .TryParse and not the Convert to avoid conversion errors.
 
Share this answer
 
v3
Comments
ridoy 15-Aug-13 12:29pm    
5ed!
fjdiewornncalwe 15-Aug-13 12:56pm    
Thanks, ridoy
idenizeni 15-Aug-13 14:35pm    
I realize this solution provides a proper way to convert the type but how does it answer the question the user posted? Why is it considered a solution if it doesn't address the question? Why did ridroy 5ed! a solution that doesn't address the question?
fjdiewornncalwe 15-Aug-13 14:43pm    
You are correct. I made the mistake of thinking web grid control and not forms grid control.
thanks to you all, this is the solution

C#
private void dgvSales_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
   if (e.ColumnIndex == 2 || e.ColumnIndex == 4)
   {
  double a = Convert.ToDouble(dgvSales.Rows[e.RowIndex].Cells["Quantity Sold"].Value);
  double b = Convert.ToDouble(dgvSales.Rows[e.RowIndex].Cells["Unit Price"].Value);
  dgvSales.Rows[e.RowIndex].Cells[5].Value = (a*b).ToString("0.00");
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900