table1
|Par Name.........| Par #|.......Units |LSL | USL | -----SKIP
|Diffusion.........908513100.......-... .. 0 -----99.9 -----1
Program...........908514100.......-... 99.5--- 999 -----1
AC_IC .. . . . . .908512100. . . . . . . .14-------13---------1
ACX_IC. . . . . . .98212100.. . . . . . 1 ------62----------1
table2
starttime | Product | Device | Diffusion | Program |
11/7/2013 SAF5100EL 163 -0.145712003 -0.146583006
11/7/2013 SAF5100EL 84 -0.137499005 -0.137592003
11/7/2013 SAF5100EL 44 -0.142690003 -0.143250003
11/7/2013 SAF5100EL 164 -0.139434993 -0.140459001
11/7/2013 SAF5100EL 34 -0.147183999 -0.148519993
table3
|Diffusion| | Program |
-0.145712003 -0.146583006
-0.137499005 -0.137592003
-0.142690003 -0.143250003
-0.139434993 -0.140459001
-0.147183999 -0.148519993
//dt1 contains Diffusion etc as rows
//dt2 contains diffusion etc as columns
//dt3 is the required table
table1 and table2 are compared and all parameter names present in
table 1[row] and also
in table2[column] are copied to table 3.
Now i want to display Par names which are present table1
and not in table2[ie AC_IC ACX_IC]
how to include statements to show those parameter names
DataTabl DataTe dt3 = newable();
DataRow dr = null;
for (int i = 0; i < dt1.Rows.Count; i++)
{
string col = dt1.Rows[i]["Par Name"].ToString();
if (dt2.Columns.Contains(col))
{
if (!dt3.Columns.Contains(col))
{
dt3.Columns.Add(col, typeof(string));
}
if (dt3.Rows.Count == 0)
{
for (int j = 0; j < dt2.Rows.Count; j++)
{
dr = dt3.NewRow();
dt3.Rows.Add(dr);
}
}
for (int j = 0; j < dt2.Rows.Count; j++)
{
dt3.Rows[j][col] = dt2.Rows[j][col].ToString();
}
}
}
Loading

VulpesPosted Mar 17, 2014, 2:02 PM
VulpesPosted Mar 18, 2014, 9:01 AM
What I think you will need to do is to parse the strings in table2 to double when adding rows to table3:
table3.Rows[j][col] = double.Parse(table2.Rows[j][col].ToString()); // EDITED
If some values could be null, then a more complicated expression will be needed.
Farhan ShariffPosted Mar 18, 2014, 8:43 AM
VulpesPosted Mar 18, 2014, 8:14 AM
But, in any case, you can't sort table3 before it's been built and populated.
Looking now at this part of the code:
DataTable resultDt = table3.Clone();
for (int m = 0; m < table3.Columns.Count; m++)
{
DataView dv = new DataView(table3);
dv.Sort = filterExp.Split(',')[m];
table3 = dv.ToTable();
for (int i = 0; i < table3.Rows.Count; i++)
{
if (resultDt.Rows.Count != table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows[i][m] = table3.Rows[i][m];
}
}
return resultDt;
I don't see the need to clone table3 and also to sort the view one column at a time. I'd have thought the following code would suffice:
DataView dv = table3.DefaultView;
dv.Sort = filterExp.TrimEnd(','); // get rid of trailing comma
return dv.ToTable();
Farhan ShariffPosted Mar 18, 2014, 6:33 AM
Farhan ShariffPosted Mar 18, 2014, 6:31 AM
VulpesPosted Mar 18, 2014, 6:20 AM
You'll then need to press enter to display the next excluded name.
Farhan ShariffPosted Mar 18, 2014, 5:17 AM
But more importantly the excluded parameter's are not getting looped
only one excluded parameter is being displayed
I want to display excluded parameters and also build table3 with only those parameter names present.
using else will stop table3 from building If I am not wrong.
private static DataTable CompareTwoDataTable(DataTable table1, DataTable table2)
{
List
DataTable table3 = new DataTable();
DataRow dr = null;
string filterExp = string.Empty;
for (int i = 0; i < table1.Rows.Count; i++)
{
string col = table1.Rows[i]["Par Name"].ToString();
if (table2.Columns.Contains(col))
{
if (!table3.Columns.Contains(col))
{
table3.Columns.Add(col, typeof(double));
filterExp = filterExp + col + " asc ,";
}
for (int j = 0; j < table2.Rows.Count; j++)
{
if (table3.Rows.Count != table2.Rows.Count)
{
dr = table3.NewRow();
table3.Rows.Add(dr);
}
table3.Rows[j][col] = (table2.Rows[j][col]);
}
}
else
{
excludedNames.Add(col);
}
}
Console.WriteLine("The following Par names were excluded from dt3\n");
foreach (string name in excludedNames)
{
Console.WriteLine(name);
Console.ReadLine();
}
DataTable resultDt = table3.Clone();
for (int m = 0; m < table3.Columns.Count; m++)
{
DataView dv = new DataView(table3);
dv.Sort = filterExp.Split(',')[m];
table3 = dv.ToTable();
for (int i = 0; i < table3.Rows.Count; i++)
{
if (resultDt.Rows.Count != table3.Rows.Count)
{
resultDt.Rows.Add();
}
resultDt.Rows[i][m] = table3.Rows[i][m];
}
}
return resultDt;
}