读取.csv以读取数据表并填充datagridview [英] Read .csv to datatable and fill a datagridview

查看:873
本文介绍了读取.csv以读取数据表并填充datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,我想将其读入datagridview(每个值放入每一列). 我阅读了带有注释的文件,然后看到每个值都除以;"

I have a .csv file and I'd like to read it into a datagridview (each value into an each column). I read this file with block note and I see that each value is divided by ";"

我试图设置一个数据表,但是它不起作用.这是我的代码:

I tried to set a datatable but it's not working. This is my code:

string FileName = @"C:\mydir\testcsv.csv";

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + Path.GetDirectoryName(FileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
conn.Open();

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + Path.GetFileName(FileName), conn);    
DataSet ds = new DataSet("Temp");
adapter.Fill(ds);

conn.Close();

dataGridView2.DataSource = ds;

我不知道哪里出了错.

推荐答案

您的代码按原样为我工作.

Your code worked for me as it is.

在查看数据集后,我仅向数据源分配添加了一行,我看到其中只有一个表名为"Table",因此我为datagridview分配了数据成员:

I just added one line to the datasource assignment after looking inside the dataset, I saw just one table is inside with name "Table" so I assigned the datamember of the datagridview:

        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "Table";

无论如何,如果我使用';'分隔符,所有值都在一列中...使用','逗号分隔符可以正常工作.

Anyway if I used ';' separator, all the values were in one column... With ',' comma separator it works ok.

表格的完整代码:

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string FileName = @"C:\mydir\testcsv.csv";

            OleDbConnection conn = new OleDbConnection
                   ("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " +
                     Path.GetDirectoryName(FileName) +
                     "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");

            conn.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter
                   ("SELECT * FROM " + Path.GetFileName(FileName), conn);

            DataSet ds = new DataSet("Temp");
            adapter.Fill(ds);

            conn.Close();

            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Table";
        }
    }
}

csv文件的内容:

abc,123
def,456
ijk,789
lmn,111213

对于以分号分隔的文件,您需要在包含csv文件的文件夹中添加一个ini文件.确切的操作方法如下:

For semicolon delimited files you need to add an ini file in your folder containing the csv file. How to do it exactly is described here:

如何在C#中通过OLEDB导入CSV文件时指定定界符

对于十进制分隔符,您必须添加

For decimal delimiter symbol you have to add the

DecimalSymbol

指向您的Jet ini文件的指令.

directive to your Jet ini file.

请参阅MSDN( 查看全文

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