Click here to Skip to main content
15,879,053 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
int qty = 0;
int amount = 0;
int rowtotal = 0;
int subtotal = 0;
int total = 0;

foreach (DataGridViewRow row in itemgridview.Rows)
{
    qty = Convert.ToInt32(row.Cells[1].Value);
    amount = Convert.ToInt32(row.Cells[2].Value);
    rowtotal = qty * amount;
    row.Cells[3].Value = rowtotal;
    subtotal += rowtotal;

}

total = subtotal;
txtordertotal.Text = total.ToString();`

please help me thank you in advance grid view cells automatically adding 0 at the foot of subtotal grid view in my c#? here is my piece of code i have attached the image please feel free to take a look :) http://s13.postimage.org/95vh13rfr/error.jpg[^]
Posted
Updated 23-Jan-13 8:40am
v4
Comments
BC @ CV 23-Jan-13 13:56pm    
Where is the textbox txtordertotal? From your image It looks like you are expecting to see this total a cell (row[3], Cell[3]), which of course your code is not doing.
zakirox123 23-Jan-13 14:01pm    
sorry i made a mistake while i am cropping the image now here is the full image take a look http://s8.postimage.org/t2exkfd45/error.jpg
BC @ CV 23-Jan-13 14:03pm    
Ok... The last I checked 8 + 12 is 20.
zakirox123 23-Jan-13 14:04pm    
i did this it worked like charm `AllowUsersToAddRow = false`
Sergey Alexandrovich Kryukov 23-Jan-13 14:05pm    
Is the case closed? See also my recommendations below.
—SA

1 solution

C#
int i = 0;
foreach (DataGridViewRow dgvr in itemgridview.Rows)
{
    if (dgvr.IsNewRow)
    {
        // do nothing
    }
    else
    {
        int j = 0;
        if (int.TryParse(dgvr.Cells[3].Value + "", out j))
        {
            i += j;
        }
        else
        {
            MessageBox.Show("wrong value at row " + (dgvr.Index + 1));
            return;
        }
    }
}
txtordertotal.Text = i.ToString();
 
Share this answer
 
v3
Comments
zakirox123 24-Jan-13 1:50am    
your solution is not that much clear i want to calculate like this qty*price=total
qty*price have to be shown in the coll[3] as "subtotal" and finally in textBox "txttotal" has to be shown as grand total.
adriancs 24-Jan-13 2:05am    
you have missed the point.

I do not provide full solution, in stead, I give you a very important clue/hints about what is causing your problem.

Your problem is your code is taking the last row into calculation. In your case, you have to check the last row is NEW ROW or not. because last row is always contains no data. Therefore, you have to use this

dgvr.IsNewRow

to by pass the last row, which is also the empty row. Then your calculation shall avoid any errors.

for the rest of the calculation. You are good to do it yourself without me showing the codes.
adriancs 24-Jan-13 2:06am    
and I also show you the extra code about the idea of using

int.TryParse()
adriancs 24-Jan-13 2:07am    
this

AllowUsersToAddRow = false

can avoid calculation errors, which an empty row will not exist, but it also lost the ability to allow your user key in data dynamically/directly into datagridview. However, to allow or not. Its up to you.
adriancs 24-Jan-13 2:22am    
the full code is not posted. in stead of giving you full baked code. This is actually good for you. In this case, you will learn more. It trains you to observe and train your programming logic. But, if you still fail to analyse the codes or integrate your code, you are welcome to leave comments and request for further help or further clarification.

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