DATAGRIDVIEW multipling two column

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.
LVL 6
MuhammadAdilAsked:
Who is Participating?
 
e1vCommented:
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
 
NeoEGMCommented:
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
 
vb_jonasCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
GarylsCommented:
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
 
NeoEGMCommented:
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
 
vb_jonasCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.