如何添加具有不同行值的新行?
foreach (DataRow row in dt.Rows)
{
lstData.Add(new Tablelist() { FileName=csv, process = row["process"] + "", Event = row["Event"] + "", status = row["status"] + "" });
}
}
DataTable dtoutput = new DataTable();
var columnNames = lstData.Select(k => k.FileName).Distinct();
foreach (string columnName in columnNames)
dtoutput.Columns.Add(columnName, typeof(string));
DataRow newrowPassCount = dtoutput.NewRow();
foreach (string columnName in columnNames) {
int count = lstData.Where(k=> k.FileName == columnName).Count(k => k.status == "Pass");
newrowPassCount[columnName] = count;
}
dtoutput.Rows.Add(newrowPassCount);
HI ...目前是最后一个代码dtoutput.Rows.Add(newrowPassCount);
将使用(newrowpasscount)值创建一行....在第二次循环之后,我需要使用不同的(newrowpasscount)值创建另一行..它现在会做什么它将覆盖上一行..我一直在尝试这个
HI...currently the last code dtoutput.Rows.Add(newrowPassCount);
will create just one row with the (newrowpasscount) value....after second loop I need another row to be created with a different (newrowpasscount) value.. what it does now it will overwrite the previous row..I've been trying this
foreach (DataRow row in dtoutput.Rows)
{
dtoutput.Rows.Add(newrowPassCount);
}
但它不起作用...
but it doesn't work...
在下面的代码中将FileName = csv作为静态值并且
In below code u have put FileName=csv as static value and
var columnNames = lstData.Select(k => k.FileName).Distinct();
总是返回一个值csv。
所以每次将值设置为相同的列名。你需要
保持
always returns one value of "csv".
so every time it set the value to same column name. and u needs to
keep
DataRow newrowPassCount = dtoutput.NewRow();
在foreach循环中
让我知道它是否有帮助。
inside foreach loop
let me know if its help or not.
您需要将 dtoutput.NewRow();
移动到foreach循环,以便每次创建的新行都添加到集合中。
You need to move the dtoutput.NewRow();
to the foreach loop so that each time the new row created are added to the collection.
DataTable dtoutput = new DataTable();
// generate the data you want to insert
DataRow toInsert = ...;
// insert in the desired place
dt.Rows.InsertAt(toInsert, index);
试一试。这可能会有所帮助。
Give it a try.This may help.