.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

ผมอยากเรียก textfile ขึ้นมาโชว์ใน ดาต้ากริด ครับ อาจารย์ ช่วยหน่อยครับ Options · View
herocom
Posted: Thursday, August 21, 2008 12:41:59 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 8/4/2008
Posts: 6
Location: thai

textfile ขึ้นมาโชว์ใน ดาต้ากริด ครับ อาจารย์ ช่วยหน่อยครับ

paedotnet
Posted: Thursday, August 21, 2008 10:11:31 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

ในหน้า 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]
herocom
Posted: Sunday, August 24, 2008 11:04:11 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 8/4/2008
Posts: 6
Location: thai

ขอบคุณ ท่านมือเทพมาก ครับ

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by