0
对不起,我的英语。我是c#中的新手。我有quuestion。我有一个数据文件,TXT:从* .txt导入不带2列的DataGridView
20160101 PL01 000000000000000003 PL02 TO 0000000001 1.720 0000000001 0000000002
Finnaly我想这个数据导入的DataGridView但只列1,4,7,8,9没有列2,3,5和6
我尝试开始导入所有数据,但我有错误
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
System.IO.StreamReader file = new System.IO.StreamReader("C:\\Users\\terrazo\\Desktop\\test1.txt");
string[] columnnames = file.ReadLine().Split(' ');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
DataRow dr = dt.NewRow();
string[] values = newline.Split(' ');
for (int i = 0; i < values.Length; i++)
{
dr[i] = values[i];
}
dt.Rows.Add(dr);
}
file.Close();
dataGridView1.DataSource = dt;
}
}
}
任何人都可以帮助我如何从txt文件导入数据没有几列? thx所有答案。
“***我尝试开始导入所有数据,但我有错误***”,那么它是什么 –
当我开始我有错误messege:未处理System.Data.dll中发生类型'System.IndexOutOfRangeException'的异常 其他信息:找不到第8列。 – terrazo
您的数据中是否有空格? –