salut à tous,
j' ai ecrit un code qui devrait pouvoir lire mon fichier et le mettre dans un tableau mais je recois un message d' erreur.
mon fichier est un fichier CSV et ressemble à quelque chose de ce genre:
110;SG
111;FG
115;KL
.
.
.
je voudrais pour voir mettre les numéros dans une colonnes et les lettres dans une autre colonne.c' est à dire
110 SG
111 FG
115 KL
.
.
.
mais je recois un message d'erreur
qui me dit "String input was not in a correct format"
je ne sais pas comment changer ca
est ce que vous pourrez m' aider S'il vous plait?
voilà mon code (j'ai pris une partie sur ce site meme ):
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Text;
namespace fichier
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 40);
this.dataGrid1.Name = "datgrid1";
this.dataGrid1.Size = new System.Drawing.Size(400, 400);
this.dataGrid1.TabIndex = 0;
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 425);
//this.Controls.Add(this.textBox1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
// the main entry point for the application
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
// on crée une table
DataTable dt = new DataTable("test");
// on crée des colonnes
dt.Columns.Add("TelephoneNumber", System.Type.GetType("System.Int32"));
dt.Columns.Add("i1", System.Type.GetType("System.Int32"));
dt.Columns.Add("User", System.Type.GetType("System.String"));
//if (File.Exists(Application.StartupPath + "\\ Recordings.csv"))
//{
StreamReader fichier = File.OpenText("h:\\ExportTeilnehmer.CSV");
while (fichier.Peek() != 0)
{
// on lit une ligne et on ajoute
string ligne = fichier.ReadLine();
string[] vals = ligne.Split(';');
DataRow dr = dt.NewRow();
dr["TelephoneNumber"] = int.Parse(vals[0]);
dr["i1"] = int.Parse(vals[1]);
dr["User"] = vals[2];
// on ajoute la ligne
dt.Rows.Add(dr);
Console.ReadLine();
}
fichier.Close();
// on genere le tableau
dataGrid1.DataSource = dt;
//dataGrid1.Refresh();
}
}
}
l' erreur s' affiche à ce niveau:
dr["TelephoneNumber"] = int.Parse(vals[0]);
dr["i1"] = int.Parse(vals[1]);
dr["User"] = vals[2];
merci pour votre aide
cordialement