Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
anybody can help me
I use foreach
C#
foreach (DataGridViewRow r in dgv_item.Rows)
{
  Amount = Amount - Convert.ToDecimal(r.Cells["Amount"].Value.ToString());
}

but in this code compile more times

i want only one time value minus from Amount...........
Posted
Updated 18-Aug-13 23:20pm
v4
Comments
Joezer BH 19-Aug-13 5:16am    
Then do a sum and at the end only one time do:
Amount -= sum;
Ashutosh_jha 19-Aug-13 5:20am    
sir how can i use Amount "Convert.ToDecimal(r.Cells["Amount"].Value.ToString());" that comes from DGV...
CodeBlack 19-Aug-13 5:22am    
can you post your whole code ?
Maciej Los 19-Aug-13 15:20pm    
Your question is too vague. Please, be more specific and provide more details...
What is a datasource for DGV?

var TotalAmount = (decimal)(from DataGridViewRow dgv_item in dgv_item.Rows
              select Convert.ToDecimal(((TextBox)dgv_item.FindControl("txtAmount")).Text)).Sum();

    Amount = Amount - TotalAmount;



hope this help .
 
Share this answer
 
v2
you have a couple of solutions that you could use

1. for loop
C#
int Amount = 0;
for(int i = 0; i < dgv_item.Rows.Count(); i++)
{
  Amount -= Convert.ToDecimal(dgv_items.Row[i].Cells["Amount"].Value);
}


or LinQ

How to calculate sum of a DataTable's Column in LINQ (to Dataset)?[^]
 
Share this answer
 
Comments
Ashutosh_jha 19-Aug-13 5:21am    
thanks
Ashutosh_jha 19-Aug-13 5:24am    
'System.Windows.Forms.DataGridViewRowCollection.Count' is a 'property' but is used like a 'method'
there is an error

Simon_Whale 19-Aug-13 5:28am    
sorry just.. it wass a bit of guess work on that line just change dgv_item.Row.Count() to

dgv_item.Row.Count
It's not that obvious what exactly you want, but there are a couple of things which may help you.
First off, a foreach loop executes on each and every value in the collection (in this case, each row in the data grid view) unless you explicitly use break to exit the loop earlier. So if you don't want every item, then you need to look at either not using a loop at all, or using some kind of conditional test inside the loop to decide when it is necessary to exit early.
For example:
C#
foreach (DataGridViewRow r in dgv_item.Rows)
    {
    decimal d = Convert.ToDecimal(r.Cells["Amount"].Value.ToString());
    if (d < 0)
        {
        break;
        }
    Amount -= d;
    }
would exit the loop when the first negative amount is found.

Exactly what you need we can't say!
 
Share this answer
 
C#
int rowCount = dgv_item.RowCount;
for (int i = 0; i < rowCount; i++)
   {
     DataGridViewRow row = dgv_item.Rows[i];
     Amount = Amount - Convert.ToDecimal(row.Cells["Amount"].Value.ToString());
   }
 
Share this answer
 

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