C # Read text and display data in the Dev Control

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.