且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何计算另一列中的行?

更新时间:2022-12-09 14:52:44

你喜欢这样......

Hi try like this...
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);

         DataRow newrowFailCount = dtoutput.NewRow();
         foreach (string columnName in columnNames)
         {
             int count = lstData.Where(k => k.FileName == columnName).Count(k => k.status == "Fail");
             newrowFailCount[columnName] = count;
         }
         dtoutput.Rows.Add(newrowFailCount);

         gridview.datasource = dtoutput;
         gridview.databind();





添加新属性 FileName 在课堂上





add a new property FileName in the class

class Tablelist
{
    public string process { get; set; }
    public string Event { get; set; }
    public string status { get; set; }
    public string FileName { get; set; }
}


循环一个DataTable并计算变量中的列存储。

然后去下一个,计算相同的列并存储在另一个变量中,依此类推其他DataTables。



然后求和所有变量计数。您现在拥有总数。



创建一个DataTable并根据需要创建列,并在适当的列下显示这些数据。
Loop for one DataTable and count that column store in a Variable.
Then go for next, count the same column and store in another variable and so on for other DataTables.

Then sum all the variable counts. You now have the total count.

Create a DataTable and make columns as you need and show these data under appropriate columns.