【问题标题】:print Csv data in DataGridview C# without using olehDBconnect在 DataGridview C# 中打印 Csv 数据而不使用 olehDBconnect
【发布时间】:2017-06-13 08:23:33
【问题描述】:

我正在尝试使用我能理解的方式导入 CSV 并在 DataGridView 中显示数据。
这是到目前为止我理解它是如何工作的代码和解释。
如果我错过了理解,请纠正我。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace test2
{
    public partial class Form1 : Form
    {   
        //Open the choose GUI to choose the file that we want to import
        OpenFileDialog openFile = new OpenFileDialog();

        public Form1()
        {
            InitializeComponent();
        }


        private void Button1_Click(object sender, EventArgs e)
        {   
            //if open successfully, then apply streamReader to it
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFile.FileName);
                //read the data in the file by using readLine
                var rl = sr.ReadLine();

                // If the rl is not null, then print (is it correct?)....
                if(rl != null)
                {
                    ///code to print data
                }
            }
        }

        //filter out the csv file.
        private void Form1_Load_1(object sender, EventArgs e)
        {
            openFile.Filter = "CSV|*.csv";
        }

    }
}

现在,我正在尝试打印数据。
我知道我需要使用 DataGridView.DataSource 来打印数据(如果我错了,请纠正我)但我不知道如何申请。
那么,我的解释是真的还是我有什么要补充的???
--初学者。

示例数据图像。

【问题讨论】:

  • 一种方法是创建 DataTable,列应与 CSV 文件的内容匹配,并将 Datatable 分配给 Grid。

标签: c# csv datagridview


【解决方案1】:

您可以构建一个列表(字符串或您想要的任何数据类型)并将其用作数据源。假设您的 DataGridView 的 x:name 是 dgv

private void Button1_Click(object sender, EventArgs e)
{
    //if open successfully, then apply streamReader to it
    if (openFile.ShowDialog() == DialogResult.OK)
    {
        List<string[]> rows = System.IO.File.ReadAllLines(openFile.FileName).Select(x => x.Split(',')).ToList();
        System.Data.DataTable dt = new System.Data.DataTable();
        List<string> headerNames = rows[0].ToList();
        foreach (var header in headerNames)
        {
            dt.Columns.Add(headers);
        }
        foreach (var x in rows.Skip(1))
        {
            if (x.SequenceEqual(headerNames))   //linq to check if 2 lists are have the same elements (perfect for strings)
                continue;     //skip the row with repeated headers
            dt.Rows.Add(x);
        }
        dgv.DataSource = dt;
    }
}

记得加using System.Linq;

请询问是否有任何部分不清楚。您可以在第二个 foreach 循环中添加更多检查(例如检查行中的所有项目是否为空)。

我的回答基于:Faster way of reading csv to grid

检查 2 个列表是否相等的最佳方法: Check if two lists are equal

【讨论】:

  • 打印出来 |长度 | ,我觉得有问题 | 340 |
  • 尝试编辑的代码,我没有意识到它只读取了第一行。这应该得到所有行。但是,由于我不知道您的 csv 格式,因此此代码假定只有 1 列。如果您提供列数或列名,我可以更好地为您完善我的答案。
  • 假设如果有不同列数和行数的不同csv文件,我该怎么办???我想让每个 Csv 文件都“灵活”。
  • 我必须知道为什么它输出数据,参考我刚刚更新的问题。
  • 啊,我明白了,您希望将标题名称保留为第一行并动态填充 DataGridView。让我修改我的答案以适应它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多