• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

manipulating dataset??

I have this data set like this

Date    Total_A Total_B   Total_C
2006-02   20      10            5    
2006-04     10       5            20
2006-10    5          25           15

I want to create a new dataset. Basically I am trying to calculate cumulativ count.
Date    Total_A Total_B   Total_C
2006-02   20      10            5    
2006-04   30      15            25
2006-10   35      40           40
 
Any idea I can do this easily.
This is what I am tried to do
-create a new dataset, newDs
-create a new table, newTable
-create a new row, newRow
-add row to table
-add table to dataset.

for each row in myDs.table(0).rows
    newRow = myDs.table(0).newRow

   for i = 0 to myDs.table(0).Rows.count
         if (i ==0) then
          newRow.Item(i) =  row.item(i)
         else
          newRow.Item(i) =  row.item(i) + newRow.item(i-1)
          end if
  End for
  myDs.table(0).add(newRow)
End each

I haven't tried this yet.
Does this look right to you?


0
dkim18
Asked:
dkim18
  • 2
2 Solutions
 
Type25Commented:
Yep looks about right.

/c# developer here tho :)
0
 
biodoomsCommented:
Hey,

I think I can write you something wich does what you wanted. Hope its usefull.

DataTable newTable = new DataTable();
newTable.Columns.Add(new DataColumn("Item1", System.Type.GetType("System.String")));
newTable.Columns.Add(new DataColumn("Item2", System.Type.GetType("System.String")));
newTable.Columns.Add(new DataColumn("Item3", System.Type.GetType("System.String")));

DataRow dr = dt.NewRow();

Int Counter;

foreach (DataRow dataRow in Datatable.Rows)
{
    If (Session["Item1"] != Null)
    {
        Counter = Convert.ToInt(Session["Item1"]) + Convert.ToInt(dataRow.ItemArray.GetValue(1));
        dr["Item1"] = Convert.ToString();
    }
    else
    {
        dr["Item1"] = dataRow.ItemArray.GetValue(1).ToString();
    }
    If (Session["Item2"] != Null)
    {
        Counter = Convert.ToInt(Session["Item2"]) + Convert.ToInt(dataRow.ItemArray.GetValue(2));
        dr["Item1"] = Convert.ToString();
    }
    else
    {
        dr["Item2"] = dataRow.ItemArray.GetValue(2).ToString();
    }
    If (Session["Item3"] != Null)
    {
        Counter = Convert.ToInt(Session["Item3"]) + Convert.ToInt(dataRow.ItemArray.GetValue(3));
        dr["Item1"] = Convert.ToString();
    }
    else
    {
        dr["Item3"] = dataRow.ItemArray.GetValue(3).ToString();
    }
    newTable.Rows.Add(dr);

    Session["Item1"] = dataRow.ItemArray.GetValue(1).ToString();
    Session["Item2"] = dataRow.ItemArray.GetValue(2).ToString();
    Session["Item3"] = dataRow.ItemArray.GetValue(3).ToString();
}

This basicly checks if there is a session. If there is a session then the new rows data is first row + session. else its just the Default row(like in the first row). And adds that to a new table. Well you can costum it as you wish but i hope it helps you a bit.

With kind regards,

Kevin Hendricks
0
 
dkim18Author Commented:
What's this Session agaiN?
Why are you using that?
Thanks much
0
 
biodoomsCommented:
Cause the loop takes a row after another and I want the previous row data and the present row data. so I session it and use it if its available.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now