Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 Smile | :) http://s13.postimage.org/95vh13rfr/error.jpg[^]
Posted 23-Jan-13 7:31am
Edited 23-Jan-13 8:40am
v4
Comments
BC @ CV at 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 at 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 at 23-Jan-13 14:03pm
   
Ok... The last I checked 8 + 12 is 20.
zakirox123 at 23-Jan-13 14:04pm
   
i did this it worked like charm `AllowUsersToAddRow = false`
Sergey Alexandrovich Kryukov at 23-Jan-13 14:05pm
   
Is the case closed? See also my recommendations below.
—SA
jibesh at 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 at 23-Jan-13 14:06pm
   
image http://s13.postimage.org/x7m47nelj/image.jpg
Sergey Alexandrovich Kryukov at 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...
—SA
zakirox123 at 23-Jan-13 14:09pm
   
please get me a sample peace of code, with in single line is enough ;)
Sergey Alexandrovich Kryukov at 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...
—SA
zakirox123 at 24-Jan-13 1:39am
   
ok thank you very much for your help here after i'll follow that rules :)
Sergey Alexandrovich Kryukov at 24-Jan-13 1:56am
   
Great.
—SA
Sandeep Mewara at 23-Jan-13 14:29pm
   
Are you still looking for something? Not clear. :doh:
jibesh at 23-Jan-13 14:46pm
   
He solved his problem like this
"i did this it worked like charm `AllowUsersToAddRow = false`"
zakirox123 at 24-Jan-13 1:41am
   
any way i solved my problem finally by disabling `AllowUsersToAddRow = false` thank you all of you help
zakirox123 at 24-Jan-13 1:42am
   
any way i solved my problem finally by disabling `AllowUsersToAddRow = false` thank you all of your help experts ;)
Sergey Alexandrovich Kryukov at 8-Feb-13 14:08pm
   
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.
—SA

1 solution

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

Solution 1

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();
  Permalink  
v3
Comments
zakirox123 at 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 at 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 at 24-Jan-13 2:06am
   
and I also show you the extra code about the idea of using
 
int.TryParse()
adriancs at 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 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
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