Click here to Skip to main content
15,913,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview in form1, when I click it want the values to be multiplied together and shown in form2.

However,
When i run the code, the cell 'c3' does not display the value of 'C1 * C2' and is empty

private void BoMDGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    foreach (DataGridView row in BoMDGV.Rows)
    {
        if (PrdtCmbBox.SelectedText == "Golf Club")
        {
            int C1 = Convert.ToInt32(BoMDGV.CurrentRow.Cells[1].Value);
            int C2 = Convert.ToInt32(BoMDGV.CurrentRow.Cells[2].Value);
            int C3 = C1 * C2;
            BoMDGV.CurrentRow.Cells[3].Value = C3;
        }
    }


What I have tried:

int s = Convert.ToInt32(BoMDGV.CurrentRow.Cells[1].Value);
int s1 = Convert.ToInt32(BoMDGV.CurrentRow.Cells[2].Value);
int s13 = s1 * s;
BoMDGV.CurrentRow.Cells[3].Value = s13;
Posted
Updated 9-Apr-18 9:43am
v3

This code is weird, in the for-each loop, you define the variable row but you don't use it in the loop. Said otherwise, for each rows of the gridview, you repeat the same code which do not depend on that row.
C#
foreach (DataGridView row in BoMDGV.Rows)
{
    if (PrdtCmbBox.SelectedText == "Golf Club")
    {
        int C1 = Convert.ToInt32(BoMDGV.CurrentRow.Cells[1].Value);
        int C2 = Convert.ToInt32(BoMDGV.CurrentRow.Cells[2].Value);
        int C3 = C1 * C2;
        BoMDGV.CurrentRow.Cells[3].Value = C3;
    }
}

-----
Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Quote:
There is no error or debugger coming up when I run the code! Do you have a solution for that then?

Your error is a logical error, not the kind of error you see at compile time.
The debugger don't come up by itself, you have to activate it.
Details depend on your IDE (visual Studio)
More or less, you set a breakpoint where you want to see what is going on, you launch your app, and the debugger will pop when execution reach the breakpoint, and then you will be able to check every variables.
Find a tutorial for more details about debugger usage.
 
Share this answer
 
v3
Comments
BillWoodruff 9-Apr-18 20:21pm    
Voted #1: this is not a solution, but, another example of your abusing this Forum by cutting and pasting the same generic advice.
Patrice T 9-Apr-18 20:49pm    
Noted. will improve the solution.
What do you mean with it doesn't have a value? like "0" or null?? it would make a difference if you add a value for "s" and "s1". wouldn't you think?
 
Share this answer
 
v2
Comments
Member 13765884 9-Apr-18 14:51pm    
the cell is left empty
Member 13770588 9-Apr-18 14:53pm    
yes, add a value for c1 and c2. or convert c3 to int32

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