Home > Net >  How c # read TXT file into the DataGridView and displays, is more than multi-line column configurati
How c # read TXT file into the DataGridView and displays, is more than multi-line column configurati

Time:10-02

O great god ah,
 
Using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
Using System. The Data. SqlClient.
Using System. IO;

The namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}

Private void button1_Click (object sender, EventArgs e)
{
Using (OpenFileDialog dlgText=new OpenFileDialog ())
{
DlgText. Filter="text file | *. TXT";
If (dlgText ShowDialog ()==DialogResult. OK)
{
//create a datatable used to hold data read
DataTable dt=new DataTable();
//add three columns to the datatable
Dt. Columns. The Add (" ID ", typeof (String));
Dt. Columns. The Add (" Name ", typeof (String));
Dt. Columns. The Add (" level ", typeof (String));
Dt. Columns. The Add (" nextlevelexp typeof (String));
Dt. Columns. The Add (" BodyRadius typeof (String));
Dt. Columns. The Add (" BaseWalkSpeed typeof (String));
Dt. Columns. The Add (" BaseMaxHp typeof (String));
Dt. Columns. The Add (" BaseAtkSpeed typeof (String));
Dt. Columns. The Add (" BaseNormalAtk typeof (String));
Dt. Columns. The Add (" BaseNormalDef typeof (String));
Dt. Columns. The Add (" BaseHitValue typeof (String));
Dt. Columns. The Add (" BaseDodgeValue typeof (String));
Dt. Columns. The Add (" BaseCritValue typeof (String));
Dt. Columns. The Add (" BaseCritAvoidValue typeof (String));

//read the file
Using (StreamReader reader=new StreamReader (dlgText FileName, Encoding. The Default))
{
//loop reads all rows
while (! Reader. EndOfStream)
{
//will each row data, using - divided into 3 section
String [] data=(https://bbs.csdn.net/topics/reader.ReadLine). The Replace (" ", "-"). The Split (' - ');
//on a new line, and will read data segmentation, respectively into three corresponding column
If (data. The Length==14)
{
DataRow Dr=dt. NewRow ();
//int index=dt. Rows. The Add ();

Dr [0]=data [0];
Dr [1]=data [1];
Dr [2]=data [2];
Dr [3]=data [3].
Dr [4]=data [4];
Dr [5]=data [5];
Dr [6]=data [6].
Dr [7]=data [7];
Dr [8]=data [8];
Dr [9]=data [9];
Dr [10]=data [10].
Dr [11]=data [11];
Dr [12]=data [12].
Dr [13]=data [13].

//the row data join the datatable
Dt. Rows. The Add (Dr);
}
DataGridView1. The DataSource=dt;
}
}
//will be displayed on the datatable binding to the datagridview results
Enclosing dataGridView1. The DataSource=dt;
}
}
}


Private void dataGridView1_CellContentClick (object sender, DataGridViewCellEventArgs e)
{
//dataGridview1. Rows. The add (item [index])
}

Private void dataGridView1_CellContentClick_1 (object sender, DataGridViewCellEventArgs e)
{

}
}
}

CodePudding user response:

There are many lines, many columns, what to read

CodePudding user response:

The contents of a file. The readAll can directly read the whole file
Each line in the split space data,
Looks like you have achieved, why do you ask

CodePudding user response:

Read but not straight, I'm going to read a lot of a TXT file, the only one, and I show no columns of data

CodePudding user response:

To open a casually excel file to display tabular form, application configuration file that game

CodePudding user response:

reference Karail reply: 3/f
read but not straight, I'm going to read a lot of a TXT file, the only one, and I show no column data
documents were just read a lot of
 List List=new List (a);//the list to store all the TXT file path 
String [] files=System. IO. Directory. GetFiles (txtPath);//txtPath for TXT path
The foreach (string STR in files)
{
String [] temp=STR. The Split (new [] {" and "}, StringSplitOptions. None).
If (temp [1]=="PDF")
{
List. The Add (STR);
}
}
for(int i=0; i{
//each file read
}
we don't know what is your TXT content format, about the break up, look at your format, online just looking for a sample

CodePudding user response:

reference 5 floor u011981242 reply:
Quote: refer to the third floor Karail response:

Read but not straight, I'm going to read a lot of a TXT file, the only one, and I show no column data
documents were just read a lot of
 List List=new List (a);//the list to store all the TXT file path 
String [] files=System. IO. Directory. GetFiles (txtPath);//txtPath for TXT path
The foreach (string STR in files)
{
String [] temp=STR. The Split (new [] {" and "}, StringSplitOptions. None).
If (temp [1]=="PDF")
{
List. The Add (STR);
}
}
for(int i=0; i{
//each file read
}
we don't know what is your TXT content format, about the break up, see your format, online looking for example
"PDF" instead of "TXT"

CodePudding user response:

Hello, I read it according to you to TXT file, can be read at the beginning, but I found another path will not be able to read, this is how to return a responsibility?

CodePudding user response:

With a number, can be loop read TXT file contents, but also easy to use cycle on the Datagridview, but in a TXT file processing is very boring, not change the database,
  •  Tags:  
  • C#
  • Related