c#csv计算文件或datagridview

时间:2017-04-20 12:49:36

标签: c# csv datagridview

我有一个csv文件,想要计算2.列多少次包含111。

csv文件有46个分隔列和分隔符;

    "first col"  "second col" "....."
     abc          111           a
     abc          112           b
     abc          113           c
     abc          111           d
     abc          112           e
     abc          113           f

我想数111。 首先填写datagridview fom datatable。

        dgv.DataSource = dgv_table;

        string[] raw_text = File.ReadAllLines("d:\\"+lb_csv.Text);
        string[] data_col = null;
        int x = 0;

        foreach (string text_line in raw_text)
        {

            // MessageBox.Show(text_line);
            data_col = text_line.Split(';');

            if (x == 0)
            {
                for (int i = 0; i <= data_col.Count() - 1; i++)
                {

                    dgv_table.Columns.Add(data_col[i]);
                }
                //header

                x++;
            }

            else
            {
                //data

              dgv_table.Rows.Add(data_col);
            }

我找到了很多解决方案来计算第二列指定的数据:111 但有时候我遇到了问题。

         int xCount = dgv.Rows.Cast<DataGridViewRow>().Select(row => row.Cells["second col"].Value).Where(s => s !=null && Equals(111)).Count();

        this.lb_qty.Text = xCount.ToString();

但是它为row.Cells [&#34; second col&#34;]提供了错误。值

 An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll

 Additional information: Column named second col cannot be found.

有人可以帮我解决这个问题,并获得所需的结果吗?

4 个答案:

答案 0 :(得分:2)

我建议你跳过使用DataGridView并在你的循环中使用计数器变量,就像Arkadiusz建议的那样。

如果您仍想使用DataTable,请计算如下值:

int xCount = dgv_table.Rows.Cast<DataRow>().Count(r => r["second col"] != null && r["second col"].ToString() == "111");

答案 1 :(得分:1)

我会尝试将该文件读入DataTable并将其用作DataSource的{​​{1}}。

DataGridView

要计算第二列中包含111的行,您可以选择DataTable d_Table = new DataTable(); //fill the DataTable this.dgv_table.DataSource = d_Table; ,如下所示:

DataTable

或者您可以在DataTable d_Table = new DataTable(); //fill the DataTable DataRow[] rowCount = d_Table.Select("secondCol = '111'"); this.lb_qty.Text = rowCount.Length.ToString(); 循环中执行此操作:

foreach

答案 2 :(得分:0)

 ArrayList<MyInterface> myList = new ArrayList<>();
    myList.add(new MyInterface()
    {
        @Override
        public String string()
        {
            return null;
        }

        @Override
        public int integer()
        {
            return null;
        }
    });

编辑: StreamReader.ReadLine()读取行但是,使用这种方法我们跳到第二行。如果没有更多的行返回null,那么这就是我们的结束条件。我认为其余的代码是可读的 :)
没有测试,所以要小心:)
可能有必要在某些地方使用Trim()或ToUpperCase()(通常在搜索时)。

答案 3 :(得分:0)

您可以使用此方法将CSV保存到数组列表列表

public static List<string[]> readCSV(String filename)
{
    List<string[]> result = new List<string[]>();
    try
    {
        string[] line = File.ReadAllLines(filename);
        foreach (string l in line)
        {
            string[] value= vrstica.Split(',');
            result.Add(value);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: '{0}'", e);
    }
    return result;
}

每个数组都代表一个列,所以你可以使用LINQ甚至循环简单地找到任何值的频率:

foreach (var item in tmp[1].GroupBy(c => c))
                {
                    Console.WriteLine("{0} : {1}", item.Key, item.Count());
                }