Click here to Skip to main content
12,998,892 members (31,469 online)
Rate this:
Please Sign up or sign in to vote.
See more:
        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 :)[^]
Posted 23-Jan-13 7:31am
Updated 23-Jan-13 8:40am
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
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.
jibesh 23-Jan-13 14:11pm
It will but the main reason could be you change the Default/Null value in column DefaultCellStyle property. check that too.
zakirox123 23-Jan-13 14:06pm
Sergey Alexandrovich Kryukov 23-Jan-13 14:05pm
Exactly. Besides, hard-coded 3... And the sloppiness in variable declaration outside of the loop, even temporary "qty", "amount", "rawtotal", excessive "total" or "subtotal"... This is not programming. OP just needs to do it all accurately, and the bug will go.

Also, get a good habit or writing int.Parse, int.TryParse instead of "convert".

I don't think it needs further answering...
zakirox123 23-Jan-13 14:09pm
please get me a sample peace of code, with in single line is enough ;)
Sergey Alexandrovich Kryukov 23-Jan-13 14:23pm
It's not single line. It should be same code, but without hard-coding, move declarations of some variables inside loop, remove redundancy...
zakirox123 24-Jan-13 1:39am
ok thank you very much for your help here after i'll follow that rules :)
Sandeep Mewara 23-Jan-13 14:29pm
Are you still looking for something? Not clear. :doh:
jibesh 23-Jan-13 14:46pm
He solved his problem like this
"i did this it worked like charm `AllowUsersToAddRow = false`"
zakirox123 24-Jan-13 1:41am
any way i solved my problem finally by disabling `AllowUsersToAddRow = false` thank you all of you help
zakirox123 24-Jan-13 1:42am
any way i solved my problem finally by disabling `AllowUsersToAddRow = false` thank you all of your help experts ;)
Please stop posting non-answers as "solutions". It can only give you abuse reports, and abuse reports made a number of heavy abusers to lost their membership. Please be careful with that.

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

int i = 0;
foreach (DataGridViewRow dgvr in itemgridview.Rows)
    if (dgvr.IsNewRow)
        // do nothing
        int j = 0;
        if (int.TryParse(dgvr.Cells[3].Value + "", out j))
            i += j;
            MessageBox.Show("wrong value at row " + (dgvr.Index + 1));
txtordertotal.Text = i.ToString();
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


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

adriancs 24-Jan-13 2:07am

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170622.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100