PC Review


Reply
Thread Tools Rate Thread

calculation within datatable question

 
 
Mike
Guest
Posts: n/a
 
      10th Mar 2008
I'm creating a datatable (see below) that is going through each row of my
dataset and add new rows per each column created. Is there a way to store
the GrossSales figures so I can use
those numbers in for a calculation within the Expenses row?

My data for the dsSales dataset like this:

year totalGross SalesmanLastName
2005 45,000 Smith
2004 25,000 Jensen
2003 12,000 A. Smith

My data for dsExpenses is coming out like this:
year Expense SalesmanLastName
2005 4,000 Smith
2004 1500 Jensen
2003 12,000 A. Smith

is there a way to store the TotalGross and use that for a calculation within
the foreach {} loop for the dsExpense so I can have the net calculated? I
have to seperate dataset due to the data is coming from 2 seperate tables
within the database

public DataTable Results()
{
dtFigures = new DataTable();
dsSales = GetSalesValues();

foreach (DataTable dt in dsSales.Tables)
{
foreach (DataRow dr in dt.Rows)
{
dr = dtFigures.NewRow();
dr["SubHeader"] = "Sales Figures"
dr["Year"] = dr["Year"].ToString();
dtFigures.Rows.Add(dr);

dr = dtFigures.NewRow();
GrossSales = Convert.ToDecimal(dr["totalGross"]) / 1000;

dr["MetricType"] = "Gross Sales
dr["Year"] = dr["Year"].ToString();
dr["Year One"] =
Convert.ToInt32(GrossSales).ToString("c0");
dtFigures.Rows.Add(dr);
}
}

dsExpenses = AllExpenses();
foreach (DataTable dt in dsExpenses.Tables)
{
foreach (DataRow dr in dt.Rows)
{
dr = dtFigures.NewRow();
dr["SubHeader"] = "Expenses";
dr["Year"] = dr["Year"].ToString();
dtFigures.Rows.Add(dr);

dr = dtFigures.NewRow();
dr["MetricType"] = "Year to date expenses";
dr["Year"] = dr["Year"].ToString();
dr["Year One Expenses"] = Convert.ToDecimal(dr["Expense"])
/ 1000;
dtFigures.Rows.Add(dr);
}
}
}


 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10th Mar 2008
"Mike" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

> is there a way to store the TotalGross and use that for a calculation
> within the foreach {} loop for the dsExpense so I can have the net
> calculated?


Can't you just declare a module-level variable and increment it as
required...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10th Mar 2008
I have something like that and its only storing the last value, I need all
values for each year
"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mike" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> is there a way to store the TotalGross and use that for a calculation
>> within the foreach {} loop for the dsExpense so I can have the net
>> calculated?

>
> Can't you just declare a module-level variable and increment it as
> required...?
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10th Mar 2008
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>>> is there a way to store the TotalGross and use that for a calculation
>>> within the foreach {} loop for the dsExpense so I can have the net
>>> calculated?

>>
>> Can't you just declare a module-level variable and increment it as
>> required...?

>
>I have something like that and its only storing the last value, I need all
>values for each year


Are you overwriting the class-level variable each time rather than
incrementing it...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10th Mar 2008
I was trying to increment it but it was only storing the last years value.
I was trying this on Saturday actaully and I deleted the code, so I can't
show you what I had either to help out.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>>> is there a way to store the TotalGross and use that for a calculation
>>>> within the foreach {} loop for the dsExpense so I can have the net
>>>> calculated?
>>>
>>> Can't you just declare a module-level variable and increment it as
>>> required...?

>>
>>I have something like that and its only storing the last value, I need all
>>values for each year

>
> Are you overwriting the class-level variable each time rather than
> incrementing it...?
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10th Mar 2008
"Mike" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>>>>> is there a way to store the TotalGross and use that for a calculation
>>>>> within the foreach {} loop for the dsExpense so I can have the net
>>>>> calculated?
>>>>
>>>> Can't you just declare a module-level variable and increment it as
>>>> required...?
>>>
>>>I have something like that and its only storing the last value, I need
>>>all values for each year

>>
>> Are you overwriting the class-level variable each time rather than
>> incrementing it...?

>
> I was trying to increment it but it was only storing the last year's
> value.
> I was trying this on Saturday actually and I deleted the code, so I can't
> show you what I had either to help out.


If you need to store individual values per year, then you could use a
generic e.g.

Dictionary<int, decimal> dicTotals = new Dictionary<int, decimal>();

Then, within your foreach loop:

if (!dicTotals.Contains(2004)
{
dicTotals.Add(2004, MyRunningTotal);
}
else
{
dicTotals[2004] += MyRunningTotal;
}

N.B. the above is probably not particularly efficient, but should work...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      10th Mar 2008
I had something like that and it was only storing the last value.

"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mike" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>>>>> is there a way to store the TotalGross and use that for a calculation
>>>>>> within the foreach {} loop for the dsExpense so I can have the net
>>>>>> calculated?
>>>>>
>>>>> Can't you just declare a module-level variable and increment it as
>>>>> required...?
>>>>
>>>>I have something like that and its only storing the last value, I need
>>>>all values for each year
>>>
>>> Are you overwriting the class-level variable each time rather than
>>> incrementing it...?

>>
>> I was trying to increment it but it was only storing the last year's
>> value.
>> I was trying this on Saturday actually and I deleted the code, so I can't
>> show you what I had either to help out.

>
> If you need to store individual values per year, then you could use a
> generic e.g.
>
> Dictionary<int, decimal> dicTotals = new Dictionary<int, decimal>();
>
> Then, within your foreach loop:
>
> if (!dicTotals.Contains(2004)
> {
> dicTotals.Add(2004, MyRunningTotal);
> }
> else
> {
> dicTotals[2004] += MyRunningTotal;
> }
>
> N.B. the above is probably not particularly efficient, but should work...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Same question - Why use a DataTable in ASP .NET? jehugaleahsa@gmail.com Microsoft C# .NET 5 12th Aug 2008 03:21 AM
Help with an Aggregate Calculation Sum( DateTime ) in a DataTable edcha Microsoft C# .NET 2 20th Apr 2008 08:55 PM
DataTable question ApeX Microsoft C# .NET 1 24th Jan 2008 05:13 PM
C# DataTable Question Jason Huang Microsoft C# .NET 3 11th Feb 2006 09:41 AM
datatable question Microsoft C# .NET 2 1st Apr 2004 08:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:49 PM.