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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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].

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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]

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
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.