Mam takie pytanie próbuję zaimportować sobie plik txt do DataGriedView i mam problem cały czas mi się wykłada na wierszu 38 i dostaję komunikat:
Wystąpił nieobsługiwany wyjątek typu „System.IndexOutOfRangeException” w System.Data.dll
Dodatkowe informacje: Nie można odnaleźć kolumny 8
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\\pkaczmar\\Desktop\\DANE1.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;
}
}
}
Dane z pliku txt mam w formie:
20160101 PL01 000000000000000003 PL02 TO 0000000001 1.720 0000000001 0000000002
Może macie jakieś pomysły jak to zaimportować??
Drugie moje pytanie to jak widać sa to dane oddzielone tabulatorami i załóżmy chciałbym by importowało mi tylko kolumnę 1,4,7,8,9 czy można to już na etapie importu pomijać czy trzeba to najpierw załóżmy wrzucić do tablicy a potem już do bazy zapisywać bez tych kolumn których nie chce??
Z góry dziękuję za pomoc dopiero raczkuję w tym temacie także proszę o wyrozumiałość.