实例目的:
将如图domo.TXT文件导入dategridview并显示为效果,注重文件读取时的逻辑运用。
televition.cs文件
namespace televition { class televition { public televition() { } public televition(string tetime, int teno, string tename) { this.Tename = tename; this.Teno = teno; this.Tetime = tetime; } public string Tetime { get; set; } public int Teno { get; set; } public string Tename { get; set; } } }
form1.cs文件:
using System.IO; namespace televition { public partial class showthis : Form { List list = new List(); public showthis() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FileStream fs = new FileStream("demo.txt", FileMode.Open); StreamReader sr = new StreamReader(fs, Encoding.Default); this.label1.Text = sr.ReadLine(); sr.ReadLine(); string[] step = null; string content = null; while (true) { content = sr.ReadLine(); if (content == null || content.Length == 0) { break; } else { step = content.Split(Convert.ToChar("\t")); list.Add(new televition() { Tetime = step[0], Tename = step[2], Teno = int.Parse(step[1]) } ); } } this.dgv.DataSource = list; sr.Close(); fs.Close(); }