The following is an example of the text data to be read.
Inventory management-inventory transaction history
Transaction ID Transaction date commodity code
218478806 2100325K0094
218478808 2100325K0150
218478810 2100325K0145
218478812 2100325K0131
218478814 2100328K0066
The text is separated.
The FrmReaderFile. cs code is as follows:
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. IO; using System. collections; namespace sms {public partial class FrmReaderFile: Form {public FrmReaderFile () {InitializeComponent ();} private void button#click (object sender, EventArgs e) {DataTable dt = new DataTable (); // which of the following columns are generated cyclically? Dt. columns. add ("thing ID"); dt. columns. add ("date"); dt. columns. add ("Product Code"); dt. columns. add ("4"); dt. columns. add ("5"); dt. columns. add ("6 ");
String strFilePath = @ "E:/data report .txt"; if (! File. Exists (strFilePath) {MessageBox. Show ("text File not found! "," Prompt ", MessageBoxButtons. OK,
MessageBoxIcon .Error); return; } else { FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));string strLine = sr.ReadLine(); int count =0; while (strLine != null) { string[] strArry = strLine.Split(); DataRow dr = dt.NewRow(); for (int i = 0; i <= strArry.Length - 1; i++) { dr[i] = strArry[i]; } dt.Rows.Add(dr); strLine = sr.ReadLine(); } sr.Close(); fs.Close(); gridControl1.DataSource = dt; } }}
Example running effect:
Question 1: How can I read and display the data after the third row?
Question 2: How to read the data in the second row and use it as the column name?
Modifying and optimizing. If you have any good suggestions, leave a message with me: f35j10@163.com, thank you.