使用CSV作为在C#中的datagridview的数据源 [英] Using a CSV as a datasource for a datagridview in c#

查看:1232
本文介绍了使用CSV作为在C#中的datagridview的数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想成为一个DataGridView我的数据源的CSV文件,但该列标题前有3个随机的线条,这是没有必要,影响表

I have a CSV file that I want to be my data source for a datagridview, but before the column headers there are 3 random lines, which are not needed and affect the table

例如:

Username: 01   
Date: 04/02/13   
*blank*            
Source, file, date, time

我使用获得的代码CSV并把它作为DataGridView的:

The code I am using to get the CSV and use it as the datagridview:

{
   string conStr = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + Path.GetDirectoryName(Path.GetFullPath(path)) + ";Extensions=csv,txt";
   OdbcConnection conn = new OdbcConnection(conStr);

   OdbcDataAdapter da = new OdbcDataAdapter("Select * from [" + Path.GetFileName(path) + "]", conn);
   DataTable dt = new DataTable(path);
   da.Fill(dt);

   dataGridView1.DataSource = dt;

   da.Dispose();
   conn.Close();
   conn.Dispose();
}



所以基本上,我需要阅读所有的CSV为表,但删除前3行文字。 ?有没有办法做到这一点作为查询

So basically, I need to read all the CSV for the table, but delete the first 3 lines of the text. Is there a way to do this as a query?

推荐答案

您可以使用的。NET txtReader文本文件

它支持可能会派上用场以下的ConnectionString选项你

It supports the following connectionstring options that might come in handy for you


  • 跳过行

  • 的标题

  • 忽略空行

下面是一个例子连接字符串:

Here is an example connection string:

 Data Source='C:\MyFolder';Delimiter=',';Has Quotes=True;Skip Rows=0;Has Header=True;
 Comment Prefix='';Column Type=String,String,String,Int32,Boolean,String,String;
 Trim Spaces=False;Ignore Empty Lines=True;

这篇关于使用CSV作为在C#中的datagridview的数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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