如何计算另一列中的行? [英] How to count the row in another column?

查看:56
本文介绍了如何计算另一列中的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OleDbCommand cmd = new OleDbCommand();

                        cmd.Connection = con;
                        // set your file name in the below query
                        cmd.CommandText = "SELECT * FROM " + csv ;
     
                        //Open Oledb Connection to read CSV file 
                        con.Open();

                        //Create one datatable to store data from CSV file
                        DataTable dt = new DataTable();

                        // Load Data into the datatable 
                        dt.Load(cmd.ExecuteReader());
                        con.Close();

                        foreach (DataRow row in dt.Rows)
                        {
                            lstData.Add(new Tablelist() { process = row["process"] + "", Event = row["Event"] + "", status = row["status"] + "" });
                            
                        }

                        // assign the lstData to the gridview..
                        GridViewShow.DataSource = lstData;
                        GridViewShow.DataBind();



此代码允许我读取3个csv文件,即0730.csv,0830.csv,0930.csv并显示全部在gridview中有这3列:进程,事件,状态...

我可以用这种输出格式实际显示它


This code allowed me to read 3 csv files which is 0730.csv, 0830.csv,0930.csv and show all in gridview with this 3 columns : process, event, status...
can I actually show it with this output format

0730|0830|0930|
pass|pass|pass|



1)我需要显示此表,

2)通过是总计数从3 csv的状态栏中读取了多少次传递,

3):0730.csv,0830.csv,0930.csv


1)i need to show this table ,
2)pass is the total count of how many pass,
3)which is read from the "status column" of this 3 csv :0730.csv, 0830.csv,0930.csv

推荐答案

你喜欢这样......

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.


这篇关于如何计算另一列中的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆