Click here to Skip to main content
14,733,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate the data columns of row
and show the value into text box
like Amount=10
Amount=10
Amount=10
Amount=10
and i want to show into text box
i m trying this code but is sowing the last value of rows.
double Final = 0;
                      //decimal Serial = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                      decimal Quantity = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);
                      decimal Rate = Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value);
                      decimal vat = Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);

                      decimal TotalPrice = Convert.ToInt32(Quantity * Rate);

                      decimal TempVatTax = TotalPrice * Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value) / 100;

                      TotalPrice = TempVatTax + TotalPrice;
                      dataGridView1.Rows[i].Cells[5].Value = TotalPrice;


                      Final = Convert.ToDouble(TotalPrice);
                      //Final=Convert.ToDouble(txt_Total.Text+Final);
                      //Final += CInt(row.Cells(0).Text);

                      txt_Total.Text = Convert.ToString(Final);

              }



Please Tell me the solution
Posted

I assume you are using this in a loop, from the Rows[i] stuff? If so, then if you want Final to be a cumulative total, then replace "=" with "+=":
Final = Convert.ToDouble(TotalPrice);
Becomes
Final += Convert.ToDouble(TotalPrice);
   
its calculating the current cell and show double value in text box
   
double LastTotal=0;
           double Final = 0;
                     //decimal Serial = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                     decimal Quantity = Convert.ToDecimal(dataGridView1.Rows[i].Cells[0].Value);
                     decimal Rate = Convert.ToDecimal(dataGridView1.Rows[i].Cells[1].Value);
                     decimal vat = Convert.ToDecimal(dataGridView1.Rows[i].Cells[2].Value);

                     decimal TotalPrice = Convert.ToInt32(Quantity * Rate);

                     decimal TempVatTax = TotalPrice * Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value) / 100;

                     TotalPrice = TempVatTax  + TotalPrice;

                     dataGridView1.Rows[i].Cells[4].Value = TotalPrice;


                     Final = Convert.ToDouble(TotalPrice);
           //Add this Code to your COde Editor
                     if (txt_Total.Text != "")
                     {
                         LastTotal = Convert.ToDouble(txt_Total.Text);
                     }
                     txt_Total.Text = Convert.ToString(Final+LastTotal);
   
ITS WORKING BUT ITS WORKING ONLY WORKING ONLY ONE TIME IF I CLICK ONE TIME ON BUTTON I WANT CALCULATION ON CELL LEAVE.

IF I CLICK ON BUTTON AND AGAIN I CLICK ON BUTTON CALCULATION NOT ACCURATE I DOING DOUBLE OF RECORD
   

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