ในหน้า design จะมี control อยู่ 2 ตัวคือ
DataGridView , Button เมื่อคลิกที่ปุ่ม button ก็จะโหลด ไฟล์จาก textfile1.txt ขึ้นมาแสดงใน DataGridView
ให้คลิกที่ button แล้วเขียนโค้ดดังนี้
private void button1_Click(object sender, EventArgs e)
{
string textfile = @"C:/textfile1.txt";
string file = new FileInfo(@"C:/textfile1.txt").DirectoryName;
string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties=\"text;\";";
OleDbConnection con = new OleDbConnection(constr);
textfile = (new FileInfo(textfile)).Name;
string comstr = "SELECT * FROM " + textfile;
OleDbCommand com = new OleDbCommand(comstr);
com.Connection = con;
OleDbDataAdapter da = new OleDbDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
แล้วให้ เรียกใช้
using System.IO;
using System.Data.OleDb;
**ใน textfile จะมีข้อมูลดังนี้
textfile1.txt
id name age
1 a 20
2 b 30
3 c 25
ผลลัพธ์แสดงดังรูป

[With great power comes great responsibility]