Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

calculation within datatable question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike - 10 Mar 2008 12:58 GMT
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);
         }
    }
}
Mark Rae [MVP] - 10 Mar 2008 13:54 GMT
> 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...?

Signature

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

Mike - 10 Mar 2008 13:59 GMT
I have something like that and its only storing the last value, I need all
values for each year

>> 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 [MVP] - 10 Mar 2008 14:18 GMT
>>> 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
[quoted text clipped - 5 lines]
>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...?

Signature

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

Mike - 10 Mar 2008 14:33 GMT
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.

>>>> 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
[quoted text clipped - 8 lines]
> Are you overwriting the class-level variable each time rather than
> incrementing it...?
Mark Rae [MVP] - 10 Mar 2008 14:47 GMT
>>>>> 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
[quoted text clipped - 13 lines]
> 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...

Signature

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

Mike - 10 Mar 2008 15:22 GMT
I had something like that and it was only storing the last value.

>>>>>> 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
[quoted text clipped - 31 lines]
>
> N.B. the above is probably not particularly efficient, but should work...

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.