Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a Winform application with C# 4.0.
My DataTable structure:

Name: Event1
Columns:
RunTime
TimeInterval
Value1
Value2
Value3
Iterations
Mode
Resource
HTML



Example Data:(all strings)
RunTime |TimeInterval | Value1 | Value2|Value3|Iterations|Mode|Resource
0.00 | .25 | 30 | 9 | A | 1 | 300 | Run1
.25 | .005 | | | | | |Run1Delay
.255 | .25 | 30 | 9 | A | 1 | 300 | Run1
.505 | .25 | 30 | 7 | A | 50 | 100 | Run2
13.005 | .005 | | | | | | Run2Delay
13.010 | .25 | 30 | 7 | B | 1 | 100 | Run3
13.260 | .25 | 30 | 7 | B | 1 | 100 | Run3
13.51 | .25 | 30 | 7 | B | 1 | 100 | Run3
13.76 | .1 | | | | | | Run3Delay

for a any Row with iteration of 1 and followed by a delay("resource like %Delay% or Iteration is null)
Alter the row by adding time interval value from the delay and Delete the Delay row.


Any and all recommendations welcomed.
Posted
Updated 22-Apr-15 6:33am
v4
Comments
virusstorm 22-Apr-15 13:48pm    
Can you provide an example of what the final data should look like?
Katy Starkey 22-Apr-15 14:10pm    
Datatable after changes:
RunTime |TimeInterval | Value1 | Value2|Value3|Iterations|Mode|Resource
0.00 | .255 | 30 | 9 | A | 1 | 300 | Run1
.255 | .25 | 30 | 9 | A | 1 | 300 | Run1
.505 | .25 | 30 | 7 | A | 50 | 100 | Run2
13.005 | .005 | | | | | | Run2Delay
13.010 | .25 | 30 | 7 | B | 1 | 100 | Run3
13.260 | .25 | 30 | 7 | B | 1 | 100 | Run3
13.51 | .35 | 30 | 7 | B | 1 | 100 | Run3

Thank You! :-)
Gideon van Dyk 22-Apr-15 21:28pm    
Best would be to cycle throw the rows and per row that matches the iteration = 1 check do a check on the following row and then combine the values and remove the following row.

This might mean you would have to create a new target datatable.

Try this
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Runtime"));
dt.Columns.Add(new DataColumn("TimeInteral"));
dt.Columns.Add(new DataColumn("value1"));
dt.Columns.Add(new DataColumn("value2"));
dt.Columns.Add(new DataColumn("value3"));
dt.Columns.Add(new DataColumn("iteration"));
dt.Columns.Add(new DataColumn("Mode"));
dt.Columns.Add(new DataColumn("Resource"));

DataRow dr = dt.NewRow();
dr["Runtime"] = "0.00";
dr["TimeInteral"] = ".25";
dr["value1"] = "30";
dr["value2"] = "9";
dr["value3"] = "A";
dr["iteration"] = "1";
dr["Mode"] = "300";
dr["Resource"] = "Run1";

dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Runtime"] = "0.25";
dr["TimeInteral"] = ".005";
dr["value1"] = "";
dr["value2"] = "";
dr["value3"] = "";
dr["iteration"] = "";
dr["Mode"] = "";
dr["Resource"] = "Run1Delay";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Runtime"] = "0.255";
dr["TimeInteral"] = ".25";
dr["value1"] = "30";
dr["value2"] = "9";
dr["value3"] = "A";
dr["iteration"] = "1";
dr["Mode"] = "300";
dr["Resource"] = "Run1";
dt.Rows.Add(dr);

DataTable dt1 = dt.Clone();
DataRow dr1;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["Resource"].Equals("Run1Delay"))
{
int RowIndex = dt1.Rows.Count - 1;
double SumValue = Convert.ToDouble(dt1.Rows[RowIndex]["TimeInteral"]) + Convert.ToDouble(dt.Rows[i]["TimeInteral"]);
dt1.Rows[RowIndex]["TimeInteral"] = SumValue.ToString();
}
else
{
dr1 = dt1.NewRow();
dr1["Runtime"] = dt.Rows[i]["Runtime"];
dr1["TimeInteral"] = dt.Rows[i]["TimeInteral"];
dr1["value1"] = dt.Rows[i]["value1"];
dr1["value2"] = dt.Rows[i]["value2"];
dr1["value3"] = dt.Rows[i]["value3"];
dr1["iteration"] = dt.Rows[i]["iteration"];
dr1["Mode"] = dt.Rows[i]["Mode"];
dr1["Resource"] = dt.Rows[i]["Resource"];
dt1.Rows.Add(dr1);
}

}
 
Share this answer
 
Comments
Katy Starkey 23-Apr-15 11:20am    
You got it! Thanks so much!
I have one additional question, request.
the conditional statement
if (dt.Rows[i]["Resource"].Equals("Run1Delay"))
Can this be altered to search for wild card %Delay%?
The resource column can contain Run2Delay, Run3Delay, Stop2Delay and other values.
Manoj Kumar Mandal 28-Apr-15 7:43am    
I havent checked with wild characters but you can try with this code
for (int i = 0; i < dt.Rows.Count; i++)
{
int RowIndex = dt1.Rows.Count - 1;
string resource ="";

resource = RowIndex >= 0 ? dt1.Rows[RowIndex]["Resource"].ToString() : dt.Rows[0]["Resource"].ToString();
if (dt.Rows[i]["Resource"].Equals(resource + "Delay"))
{

double SumValue = Convert.ToDouble(dt1.Rows[RowIndex]["TimeInteral"]) + Convert.ToDouble(dt.Rows[i]["TimeInteral"]);
dt1.Rows[RowIndex]["TimeInteral"] = SumValue.ToString();
}
else
{
dr1 = dt1.NewRow();
dr1["Runtime"] = dt.Rows[i]["Runtime"];
dr1["TimeInteral"] = dt.Rows[i]["TimeInteral"];
dr1["value1"] = dt.Rows[i]["value1"];
dr1["value2"] = dt.Rows[i]["value2"];
dr1["value3"] = dt.Rows[i]["value3"];
dr1["iteration"] = dt.Rows[i]["iteration"];
dr1["Mode"] = dt.Rows[i]["Mode"];
dr1["Resource"] = dt.Rows[i]["Resource"];
dt1.Rows.Add(dr1);
}

}
because as i can see from your data that delay occurs with the same name of previous resource.
To change the control statement I did the following:

DataTable dt1 = dt.Clone();
DataRow dr1;
for (int i = 0; i < dt.Rows.Count; i++)
{
string delay = Convert.ToString(dt.Rows[i]["Resource"]);

if (delay.IndexOf("eXDly") != -1 {
int RowIndex = dt1.Rows.Count - 1;
double SumValue = Convert.ToDouble(dt1.Rows[RowIndex]["TimeInteral"]) + Convert.ToDouble(dt.Rows[i]["TimeInteral"]);
dt1.Rows[RowIndex]["TimeInteral"] = SumValue.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