将多个文本文件内的数据导入到Datagridview
1 private BindingList listXSxxInfoList = new BindingList(); 2 openFileDialog1.Multiselect = true;//允许选择多个文件 3 ListXSxxInfoList.Clear(); 4 string filePath = ""; 5 openFileDialog1.Filter = "文本文件*.txt|*.txt"; 6 7 if (openFileDialog1.ShowDialog() == DialogResult.OK) 8 { 9 TBFilePath.Text = filePath; 10 foreach (string s in openFileDialog1.FileNames) 11 { 12 TBFilePath.Text += s + ";"; 13 FileStream fs = new FileStream (s, FileMode.Open, FileAccess.Read, FileShare.None); 14 StreamReader sr = new StreamReader (fs, System.Text.Encoding.Default); 15 string row = sr.ReadLine(); 16 string[] rowlist = null; 17 18 Model.XSxx XSInfoListModel = null; 19 while (row != null) 20 { 21 rowlist = row.Split (','); 22 XSInfoListModel = new Model.XSxx(); 23 if (rowlist.Count() == 10) 24 { 25 XSInfoListModel.xssj = Convert.ToDateTime (rowlist[1]); 26 XSInfoListModel.jhsztm = rowlist[2]; 27 XSInfoListModel.jhsmc = rowlist[3]; 28 XSInfoListModel.zsm = rowlist[4]; 29 XSInfoListModel.spbm = rowlist[5]; 30 XSInfoListModel.spmc = rowlist[6]; 31 XSInfoListModel.xsdj = rowlist[7]; 32 XSInfoListModel.xszl = rowlist[8]; 33 XSInfoListModel.xsje = rowlist[9]; 34 listXSxxInfoList.Add (XSInfoListModel); 35 } 36 row = sr.ReadLine(); 37 } 38 sr.Close(); 39 } 40 41 Datagridview1.DataSource = null; 42 Datagridview1.DataSource = listXSxxInfoList; 43 }