Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
i'm having values like
datatable dt;

C#
dt.row[0][0]=10,
dt.row[0][1]=20,
dt.row[0][2]=30,
dt.row[0][3]=40,
dt.row[0][4]=50,
dt.row[0][5]=60, //and soon....

now i want to add all the values in the datatable of different rows of first column...

i.e 10+20+30+........


can any one tell me how to do this thing...
Posted
Updated 30-Nov-11 22:24pm
v2
Comments
[no name] 1-Dec-11 4:24am    
EDIT: added "pre" tag

There are loads of ways!
The simplest to understand is:
C#
DataTable dt;
...
DataRow dr = dt.Rows[0];
int sum = 0;
foreach (object o in dr.ItemArray)
    {
    if (o is int)
        {
        sum += (int)o;
        }
    }




C#
for (int i = 0; i < dt.Rows.Count; i++)
                {
                   if (dt.Rows[i][1].ToString() != null)
                   {
                       string yy = dt.Rows[i][1].ToString();
                       count = yy + count;
                  }

                }
"but row[0][1] value not adding with the row[1][1] value.."
Try:
C#
int count = 0;
for (int i = 0; i < dt.Rows.Count; i++)
    {
    if (dt.Rows[i][1].ToString() != null)
       {
       count += (int) dt.Rows[i][1];
       }
    }
 
Share this answer
 
v2
Comments
surikarthi 1-Dec-11 4:34am    
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][1].ToString() != null)
{
string yy = dt.Rows[i][1].ToString();
count = yy + count;
}

}

but row[0][1] value not adding with the row[1][1] value..
OriginalGriff 1-Dec-11 5:25am    
Answer updated
Its very very simple, you just need to put 2 FOR loops, 1 for rows and other for columns.
If you want addition of rows of only first column, you can use only 1 FOR loop.
 
Share this answer
 
Comments
surikarthi 1-Dec-11 4:26am    
sir already i'm following this manner..
but row[0][1] value not adding with the row[1][1] value...

for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][1].ToString() != null)
{
string yy = dt.Rows[i][1].ToString();
count = yy + count;
}

}
nagendrathecoder 1-Dec-11 4:33am    
Do you want to add those integers or make a string of those numbers?
Take that string yy outside FOR loop.
surikarthi 1-Dec-11 4:38am    
those number are dynamic we cant tell which number is coming next...

those numbers are taking from the db
nagendrathecoder 1-Dec-11 4:52am    
What is count? is it a string variable?
try this:
count = count + yy;
surikarthi 1-Dec-11 5:11am    
can u tell the correct manner...

if v write a=a+b or a=b+a both r same...
C#
var sum = dt.AsEnumerable().Sum(x=>x.Field<int>("SomeProperty"));
 
Share this answer
 
Comments
surikarthi 1-Dec-11 5:19am    
can u tell me Someproperty means.... from where ur passing this...
Anuj Banka 1-Dec-11 6:09am    
Means Your Column name which you want.Just give the column name of the table.And in one shot it will give you the sum
<pre lang="c#">
//for this question we have different type of method.check the //below simple example


DataTable table = new DataTable();
table.Columns.Add("value", typeof(int));
table.Rows.Add(10);
table.Rows.Add(20);
table.Rows.Add(30);
table.Rows.Add(40);
table.Rows.Add(50);

foreach (DataRow drr in table .Rows)
{
console.writeline(drr["value"].ToString());
}
</pre>
 
Share this answer
 
Try this:
int count = 0;
for (int i = 0; i < dt.Rows.Count; i++)
                {
                   if (dt.Rows[i][1] != null)
                   {
                       int yy = Convert.ToInt32(dt.Rows[i][1]);
                       count = count + yy;
                  }
                           
                }
 
Share this answer
 
Comments
surikarthi 1-Dec-11 5:50am    
i have try this also but i'm not getting...
nagendrathecoder 1-Dec-11 5:52am    
What error are you getting?
I tried it and i am getting perfect results.
surikarthi 1-Dec-11 5:58am    
ya its working... Thank q sir... :)

thanks to all who helped me...
nagendrathecoder 1-Dec-11 6:00am    
You welcome. Mark it as answer if this solves your problem.
surikarthi 1-Dec-11 6:01am    
before i'm writing in this way
int yy = Convert.ToInt32(dt.Rows[i][1].ToString());
that time i got this below error..

Input string was not in a correct format.

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