dorine82
Messages postés28Date d'inscriptionmardi 10 juillet 2007StatutMembreDernière intervention17 septembre 2007
-
21 août 2007 à 10:34
Liverion
Messages postés296Date d'inscriptionmardi 22 avril 2008StatutMembreDernière intervention18 août 2008
-
22 avril 2008 à 16:35
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.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]);
dorine82
Messages postés28Date d'inscriptionmardi 10 juillet 2007StatutMembreDernière intervention17 septembre 2007 21 août 2007 à 12:05
je l'ai fait et maintenant je recois un message de nullreferenceexception.
et en plus de ca mon tableau meme ne s' affiche plus
voila de nouveau mon code:
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.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)
{
try
{
// 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();
dorine82
Messages postés28Date d'inscriptionmardi 10 juillet 2007StatutMembreDernière intervention17 septembre 2007 21 août 2007 à 13:48
je l' ai fait et ca marche mais en m' affichant juste la premiere ligne de mon texte.
est ce qu' il y' aurait pas une autre commande que je devrais ajouter pour que ca m' affiche tout mon texte?