cela peut sembler être un problème simple pour beaucoup d’entre vous, mais vous voulez que j’essaie de le faire, c’est que je lis un fichier texte sur un événement click en utilisant StreamReader (ASP.net & C #) après avoir lu ce fichier texte, je divise chaque ligne avec ‘,’ délimiteur, puis chaque partie de celui-ci est stockée dans Datatable's
colonne Datatable's
puis lie le datatable à mon gridview, mon problème est que j’ai écrit le code mais que j’obtiens un gridview vide comme celui-ci
En-tête de colonne que j’ai créé à partir du concepteur avec autogeneratecolumns="false"
mon code est
protected void readfile_Click(object sender, EventArgs e) { ssortingng line; DataTable dt = new DataTable(); using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt")) { while ((line = sr.ReadLine()) != null) { ssortingng[] parts = line.Split(','); dt.Rows.Add(); for (int i = 0; i < parts.Length; i++) { dt.Columns.Add(); dt.Rows[0][i] = parts[i]; MyGridView.DataSource = dt; MyGridView.DataBind(); } } sr.Close(); }
mon fichier texte a des données
1,1,4,2,"#",Description1 5,5,4,2,"#",Description2 3,3,6,3,"#",Description3 2,2,4,2,"#",Description4 4,5,4,2,"#",Description5
J’espère que vous avez ce que j’essaie de demander.
Que diriez-vous quelque chose comme ça:
protected void readfile_Click(object sender, EventArgs e) { DataTable table = new DataTable(); table.Columns.Add("Row No."); table.Columns.Add("Col No."); table.Columns.Add("Width"); table.Columns.Add("Height"); table.Columns.Add("Image URL"); table.Columns.Add("Description"); using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt")) { while (!sr.EndOfStream) { ssortingng[] parts = sr.ReadLine().Split(','); table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5]); } } MyGridView.DataSource = table; MyGridView.DataBind(); }
Ajouter des colonnes à datatable en utilisant le code ci-dessous
DataTable dt = new DataTable(); dt.Columns.Add("Row No", typeof(Int32)); dt.Columns.Add("Col No", typeof(Int32)); dt.Columns.Add("Width", typeof(Int32)); dt.Columns.Add("Height", typeof(Int32)); dt.Columns.Add("ImageUrl", typeof(Ssortingng)); dt.Columns.Add("Description", typeof(Ssortingng));
Et liez datatable (dt) après avoir rempli toutes les lignes dans datatable, after loop
using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt")) { while ((line = sr.ReadLine()) != null) { ssortingng[] parts = line.Split(','); var dr = dt.NewRow(); //use newrow to create new row for (int i = 0; i < parts.Length; i++) { dr[i] = parts[i]; } dt.Rows.Add(dr); //add row to datatable now } sr.Close(); } //bind datatable to Gridview after we load file into dt MyGridView.DataSource = dt; MyGridView.DataBind();
DataColumn
dehors de la boucle for
DataRow
aide de la méthode DataTable.NewRow
Votre code devrait ressembler à ceci:
DataTable dt = new DataTable(); dt.Columns.Add("Row No", typeof(Int32)); dt.Columns.Add("Col No", typeof(Int32)); dt.Columns.Add("Width", typeof(Int32)); dt.Columns.Add("Height", typeof(Int32)); dt.Columns.Add("ImageUrl", typeof(Ssortingng)); dt.Columns.Add("Description", typeof(Ssortingng)); using (StreamReader sr = new StreamReader(@"D:\Temp\fileread\readtext.txt")) { while ((line = sr.ReadLine()) != null) { ssortingng[] parts = line.Split(','); var row = dt.NewRow(); for (int i = 0; i < parts.Length; i++) { row[i] = parts[i]; } // important thing! dt.Rows.Add(row); } sr.Close(); } MyGridView.DataSource = dt; MyGridView.DataBind();