Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in CSharp....

I want to multiply the value of two column and display it another column.
I have written the code as..
C#
int s = Convert.ToInt32(dataGridView1.Rows[0].Cells[0].Value);
            int s1 = Convert.ToInt32(dataGridView1.Rows[0].Cells[1].Value);
            int s13 = s1 * s;
dataGridView1.Rows[0].Cells[2].Value = s13;


bt this code work in one row. I want to do more than one row may be 5/6.

And after that I want to save the value of datagridView in a table.

Plz help me..

Thanks in advans
Posted
Updated 21-Feb-17 19:23pm
Comments
Ankit Kumar Gupta 29-Feb-12 23:19pm    
What you are binding with the grid?
dataset, or
sqlDataSource etc.

Got it. Instead of Rows[0] attribute, you should use CurrentRow. It should be fine.

C#
int s = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
            int s1 = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
            int s13 = s1 * s;
dataGridView1.CurrentRow.Cells[2].Value = s13;
 
Share this answer
 
Comments
Mithlesh Shaw 29-Feb-12 23:30pm    
I think y have not understand my problm.
This code works bt it does not giv me appropiat answer.
I have enterd three row bt the multipication shows of 3rd row.
I want to display the multipication of all three row in diffrent column.
Like this...
5 5 25
6 6 36
7 7 49
Hi,

Why you are doing this code from codebehind, instead you can use your SQL query to multiply value and display it in your third column.

like

SQL
select productID,productPrize,productSellCount, productPrize*productSellCount from products;


hope this will solve your problem.

Thanks
-Amit.
 
Share this answer
 
Comments
Mithlesh Shaw 1-Mar-12 0:09am    
I dont want to do like this. If i do like I will face problem. Plz help me.....Plz
AmitGajjar 1-Mar-12 0:12am    
what kind of problem you have. As computation in database level will be the best option. and don't say please, i will try my best to solve your problem.
Mithlesh Shaw 1-Mar-12 0:42am    
Provide me the code because I am new in programing. You have just provide me the Quary.
AmitGajjar 1-Mar-12 0:44am    
you must have SQL query or Storedprocedure to fetch your code. right ? post your SQL code so that i can modify it and postback.
Mithlesh Shaw 1-Mar-12 0:59am    
I dont have any SQLQuary. Actually a dataGridView control in my form and user will put the value in runtime after that a button is there. Then the user click the button and the Multipication of given number will show. Just like that...
5 5 25
6 6 36
7 7 49
User will provide 5& 5, 6&6, 7&7.
The multipication will generate after the click event of the button.
int A = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
int B = Convert.ToInt32(dataGridView1.CurrentRow.Cells[1].Value);
int C = A * B;
dataGridView1.CurrentRow.Cells[2].Value = C;
 
Share this answer
 
Comments
sagar Lalage 20-Jun-12 9:19am    
please give same solution for grid view
Modify Your cOde like this

for (int n = 0; n < (dataGridView1.Rows.Count - 1); n++)
{

int s = Convert.ToInt32(dataGridView1.Rows[n].Cells[0].Value);
int s1 = Convert.ToInt32(dataGridView1.Rows[n].Cells[1].Value);
int s13 = s1 + s;
dataGridView1.Rows[n].Cells[2].Value = s13;

}
 
Share this answer
 
for (int n = 0; n < (dataGridView1.Rows.Count); n++)
            {
                int s = Convert.ToInt32(dataGridView1.Rows[n].Cells[0].Value);
                int s1 = Convert.ToInt32(dataGridView1.Rows[n].Cells[1].Value);
                int s13 = s1 * s;
                dataGridView1.Rows[n].Cells[2].Value = s13;
            }
 
Share this answer
 
v2
Comments
CHill60 8-Feb-17 12:39pm    
Apart from the fact this question was answered 5 years ago, this solution you have posted is identical to Solution 4. Claiming someone else's work as your own is not tolerated on this site. It's bad code anyway as it could cause exceptions
select productID,productPrize,productSellCount, productPrize*productSellCount from products;

how to display productPrize*productSellCount into data gridview in winforms can anybody explain that
 
Share this answer
 
Comments
Mithlesh Shaw 22-Feb-17 1:53am    
Thank for your reply. But this question was posted on 1-Mar-12.

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