我使用的是关注code读取CSV文件的内容。
http://www.sendspace.com/file/l0zki3 链接下载CSV文件..
这是位于我的C CSV文件:驱动
。我得到的例外是输入数组长度超过列在这个表中的数字。为什么会出现这种情况?
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Text;
使用System.Windows.Forms的;
使用System.IO; // stremreader ..是.. FRM System.IO .. ..
命名空间csvtodatagridview
{
公共部分类Form1中:形态
{
公共Form1中()
{
的InitializeComponent();
}
私人无效ShowData()
{
布尔FLG = TRUE;
串熟食店=,;
字符串tbname =Booktb; //数据集..用了2储存。然后将数据集..格..
字符串文件名=(C: SharedIncidents.csv); // 路径。 PLZ保持相同的路径。 ..
字符串str = Convert.ToString(System.DateTime.Now);
的DataSet ds为新的DataSet();
StreamReader的SR =新的StreamReader(文件名); //输入 - 输出。功能2个读..文件..
ds.Tables.Add(tbname); //增加2台..
ds.Tables [tbname] .Columns.Add(日期); //指定。 colum1为DataGrid1中。
ds.Tables [tbname] .Columns.Add(状态); //为datagrid2指定colum2。
ds.Tables [tbname] .Columns.Add(受让人);
ds.Tables [tbname] .Columns.Add(回应);
字符串ALLDATA = sr.ReadToEnd();
字符串[]行= alldata.Split( N.ToCharArray());
的foreach(行字符串R)
{
字符串[]项目= r.Split(deli.ToCharArray());
ds.Tables [tbname] .Rows.Add(项目);
}
诠释计数= ds.Tables [tbname] .Rows.Count;
INT索引= 0;
INT CURRENTINDEX = 0;
对于(指数= 0;指数<计数;指数++)
{
如果(ds.Tables [tbname] .Rows [指数] [0] .Equals(STR))
{
FLG = TRUE;
数据集DS1 =新的DataSet();
ds1.Tables.Add(tbname);
ds1.Tables [tbname] .Columns.Add(日期); //指定。 colum1为DataGrid1中。
ds1.Tables [tbname] .Columns.Add(状态); //为datagrid2指定colum2。
ds1.Tables [tbname] .Columns.Add(受让人);
ds1.Tables [tbname] .Columns.Add(回应);
CURRENTINDEX =指数;
而(CURRENTINDEX<计数)
{
DataRow的行= ds.Tables [tbname]行[CURRENTINDEX]。
ds1.Tables [tbname] .Rows.Add(行);
CURRENTINDEX ++;
}
打破;
}
}
如果(FLG ==假)
Console.WriteLine(日期不匹配);
其他
this.dataGridView1.DataSource = ds.Tables [0] .DefaultView;
}
私人无效的button1_Click(对象发件人,EventArgs的)
{
ShowData(); //调用ABV功能。 。
}
}
}
![如何计算去重数据计数 比如,计算A列类型中数量等于6的去重个数有多少个](https://p.xsw88.cn/allimgs/daicuo/20230903/5960.png)
解决方案
硬没有看到输入数据(或者至少,它的样品)来回答。然而,这是不的正确的假设,以CSV一个逗号是指下一场;该数据可能会被引用,例如:
123,Gravell,马克,1324
我会从这里推荐 CSV阅读器,它得到这个权利。
I'm using Following code to read CSV file contents.
http://www.sendspace.com/file/l0zki3 link to download CSV file..
This is the CSV file which is located in my C: Drive.
The exception I am getting is "Input array is longer than the number of columns in this table". Why might this happen?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO; //stremreader.. is frm.. System.IO.. ..
namespace csvtodatagridview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ShowData()
{
Boolean flg=true;
string deli = ",";
string tbname = "Booktb"; //dataset .. used 2 stored. then move dataset to .. grid..
string filename = ("C:SharedIncidents.csv"); // path. plz keep the same path. ..
string str = Convert.ToString(System.DateTime.Now);
DataSet ds = new DataSet();
StreamReader sr = new StreamReader(filename); //Inputoutput .function 2 read.. file..
ds.Tables.Add(tbname); //add 2 table..
ds.Tables[tbname].Columns.Add("Date"); //specify. colum1 for datagrid1.
ds.Tables[tbname].Columns.Add("Status" );//specify colum2 for datagrid2.
ds.Tables[tbname].Columns.Add("Assignee" );
ds.Tables[tbname].Columns.Add("Response" );
string alldata = sr.ReadToEnd();
string[] rows = alldata.Split("n".ToCharArray());
foreach (string r in rows)
{
string[] items = r.Split(deli.ToCharArray());
ds.Tables[tbname].Rows.Add(items);
}
int count = ds.Tables[tbname].Rows.Count;
int index = 0;
int currentindex = 0;
for (index = 0; index < count; index++)
{
if (ds.Tables[tbname].Rows[index][0].Equals(str))
{
flg = true;
DataSet ds1 = new DataSet();
ds1.Tables.Add(tbname);
ds1.Tables[tbname].Columns.Add("Date"); //specify. colum1 for datagrid1.
ds1.Tables[tbname].Columns.Add("Status");//specify colum2 for datagrid2.
ds1.Tables[tbname].Columns.Add("Assignee");
ds1.Tables[tbname].Columns.Add("Response");
currentindex = index;
while (currentindex < count)
{
DataRow row = ds.Tables["tbname"].Rows[currentindex];
ds1.Tables[tbname].Rows.Add(row);
currentindex++;
}
break;
}
}
if (flg == false)
Console.WriteLine("Date not matched");
else
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button1_Click(object sender, EventArgs e)
{
ShowData(); // call the abv function. .
}
}
}
解决方案
Hard to answer without seeing the input data (or at least, a sample of it). However, it is not correct to assume that a "comma" in CSV means "next field"; the data might be quoted, for example:
123, "Gravell, Marc", 1324
I would recommend the CSV Reader from here, which gets this right.
相关推荐
最新文章