?
Solved

DATAGRIDVIEW multipling two column

Posted on 2006-04-22
9
Medium Priority
?
1,036 Views
Last Modified: 2008-02-07
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
Comment
Question by:MuhammadAdil
6 Comments
 
LVL 6

Accepted Solution

by:
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

by:NeoEGM
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

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

MyDataTable.Columns.Add("TotalPrice", System.Type.GetType("System.Single"), "UnitPrice*UnitsSold")

regards
Jonas
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Assisted Solution

by:Garyls
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

by:NeoEGM
ID: 16722939
The solution I wrote is entirely working and corrects two errors MuhammadAdil had made.

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

by:vb_jonas
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question