Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to multiply a textbox with a cell in the datagridview, I've tried breaking it down into a few segments, does anyone know where I've gone wrong?
C#
public int a, b, c, i;

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridViewRow dg in dataGridView1.Rows)
    {
        try
        {
            a = int.Parse(OdrQuantityTxtBx.Text);
            b = (int)dataGridView1.Rows[i].Cells[1].Value;
            c = (a * b);
                     
            c = dataGridView1.Columns[i].Cells[1].Value;
        }
        catch
        {
        }
    }
}


What I have tried:

using 'public double' for a,b,c
Posted
Updated 22-Feb-18 1:26am
v2

First off, never "swallow" exceptions - that hides problems because you don't even get told it's a problem.
Second, don't use exceptions as part of "normal processing" - instead of a try...catch block to cope with conversion failures, use TryParse which returns true or false depending on success or failure to convert the value.
Third, why are you using a loop at all, if your final result will alway be the value from the last row only?
 
Share this answer
 
1) Ditch the try-catch-block. It can detect errors only accurate to "somewhere in that block", you may need more accuracy. And just swallowing the exception in an empty catch block doesn't solve anything. It just hides the real problems and prevents you from fixing them.
2) int.TryParse(OdrQuantityTxtBx.Txt, out a) has a try-catch included. It returns a boolean you can test for successful parsing.
3) dataGridView1.Rows[i].Cells[1].Value is a string. You cannot just cast it to int. You need to incorporate 2) again.
4) c = dataGridView1.Columns[i].Cells[1].Value; overwrites your precious multiplication result from one line earlier with a cell value. I think you want it to be the other way round.
But that cell is the same you originally used for the multiplication. You may want to store the result in another cell instead. And .Columns[i] doesn't have a member named "Cells".
And I'm still assuming .Value to be of type string, so you can't assign it to an integer, nor a double.
So maybe have another column and use
dataGridView1.Rows[i].Cells[2].Value = c.ToString();
 
Share this answer
 
C#
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int a;
    int b;
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
        if (int.TryParse(OdrQuantityTxtBx.Text, out a) && 
            int.TryParse(row.Cells[1].Value.ToString(), out b))
        {
            // your code was overwriting the value that was in one of the 
            // cells you were using for the math. That seemed peculiar to 
            // me.
            row.Cells[2].Value = a * b;
        }
        else
        {
            // handle the error by either throwing an exception or displaying an 
            // error indicator in the cell that contains the result. 
        }
    }
}
 
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