有 Delimeter Character 既 Text File 格式 ("C:\stocks.txt") |
代號|名稱 0001|長江實業 0002|中電控股 0003|香港中華煤氣 0004|九龍倉集團 0005|匯豐控股 0006|電能實業 0011|恒生銀行 0012|恒基地產 0013|和記黃埔 0016|新鴻基地產 0017|新世界發展 0019|太古股份公司A 0023|東亞銀行 0066|港鐵公司 0083|信和置業 0101|恒隆地產 0144|招商局國際 0267|中信泰富 0291|華潤創業 0293|國泰航空 0330|思捷環球 0386|中國石油化工股份 0388|香港交易所 0494|利豐 0688|中國海外發展 0700|騰訊控股 0762|中國聯通 0836|華潤電力 0857|中國石油股份 0883|中國海洋石油 0939|建設銀行 0941|中國移動 1088|中國神華 1109|華潤置地 1199|中遠太平洋 1398|工商銀行 1880|百麗國際 1898|中煤能源 2038|富士康國際 2318|中國平安 2388|中銀香港 2600|中國鋁業 2628|中國人壽 3328|交通銀行 3988|中國銀行 |
讀取 有 Delimeter Character 既 Text File 後 放入 DataGridView |
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; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { get_result(); } private void get_result() { DataTable t = new DataTable(); System.IO.StreamReader sr = new System.IO.StreamReader("C:\\stocks.txt"); String line = ""; Int32 i; String[] p; line = sr.ReadLine(); p = line.Split('|'); foreach (var s in p) { t.Columns.Add(s, typeof(String)); } line = sr.ReadLine(); i = 0; while (line != null && i< 1750) { p = line.Split('|'); t.Rows.Add(p); line = sr.ReadLine(); i += 1; } this.dataGridView1.DataSource = t; } } } |