Solved

# DATAGRIDVIEW multipling two column

Posted on 2006-04-22
Medium Priority
1,036 Views
i have three column in datagridview
UnitPrice  UnitsSold TotalPrice
first two column reteriving from database.
i m try to multiplying first two column and result show in TotalPrice
i m try in this way
int b = No of rows
for (int i = 0; i == b; i++)
{
if (this.dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString() != null)
{
if (this.dataGridView1.Rows[i].Cells["UnitsSold"].Value.ToString() != null)
{
this.dataGridView1.Rows[0].Cells["TotalPrice"].Value=   (Convert.ToInt32(this.dataGridView1.Rows[0].Cells["UnitPrice"].Value.ToString()) * Convert.ToInt32(this.dataGridView1.Rows[0].Cells["UnitsSold"].Value.ToString()));
}
}
}
but no desired result.
0

LVL 6

Accepted Solution

e1v earned 500 total points
ID: 16515094
In your for expression you should have
for (int i = 0; i <= b; i++) instead of for (int i = 0; i == b; i++)

And in the loop  you will probably have .Rows[i]. instead of .Rows[0].

0

LVL 3

Assisted Solution

NeoEGM earned 500 total points
ID: 16515122
The full code should be like this:

int b = number_of_rows;

for (int i = 0; i < b; i++)     //You shoud iterate while i < b, taking into account that Rows[b] does not exist
{
if (this.dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString() != null)
{
if (this.dataGridView1.Rows[i].Cells["UnitsSold"].Value.ToString() != null)
{
this.dataGridView1.Rows[i].Cells["TotalPrice"].Value=   (Convert.ToInt32(this.dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString()) * Convert.ToInt32(this.dataGridView1.Rows[i].Cells["UnitsSold"].Value.ToString()));     //You mus work with relative rows (Rows[i]), instead of always working with the first one (Rows[0])
}
}
}

Hope it helps...

Regards,
NeoEGM
0

LVL 12

Assisted Solution

vb_jonas earned 500 total points
ID: 16517796
I would suggest you to add an expression column to your datatable, just like this:

regards
Jonas
0

LVL 1

Assisted Solution

Garyls earned 500 total points
ID: 16561193
Or if you're selecting from a database then the alternative to what Jonas stated would be to modify your SELECT statement to be something like:

SELECT UnitPrice, UnitsSold, (UnitPrice*UnitsSold) AS TotalPrice FROM [TableName]

0

LVL 3

Expert Comment

ID: 16722939

The first solution is not correct because using "<=" instead of "<" would cause the array to be consulted outside its bounds and because it doesn't correct the second error I pointed out...

Regards,
NeoEGM
0

LVL 12

Expert Comment

ID: 16723789
e1v was first but not all correct, corrected by neoegm. I and Garyls give alternative solutions. Therefore I think neo's comment should be the accepted answer and e1v assisted answer, no?
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Screencast - Getting to Know the Pipeline
###### Suggested Courses
Course of the Month15 days, 1 hour left to enroll