日期:2014-05-17 浏览次数:20733 次
private void btnOpen_Click(object sender, EventArgs e) { OpenFileDialog dlgOpenFile = new OpenFileDialog { Filter = "ICT File(*.dat)|*.dat" }; if (dlgOpenFile.ShowDialog() == DialogResult.OK) //OK表示按下了“打开” { string s = null; DataTable table = new DataTable("auto"); DataRow row = null; String[] split = null; using (StreamReader sr = new StreamReader(dlgOpenFile.FileName, false)) { s = sr.ReadLine(); split = s.Split('\t'); int i = 0; foreach (String colname in split) { table.Columns.Add(colname, System.Type.GetType("System.String")); i++; } int j = 0; while (sr.Peek() > -1) { s = sr.ReadLine(); j = 0; row = table.NewRow(); split = s.Split('\t'); foreach (String colname in split) { row[j] = colname; j++; if (j >= i) { break; } } table.Rows.Add(row); } dataGridView1.DataSource = table.DefaultView; // 循环所有字段列名不为Date的则隐藏掉 try { for (int count = 0; count < dataGridView1.Columns.Count; count++) { //if (dataGridView1.Columns[count].HeaderText.Trim() != "Date") //{ // //dataGridView1.Columns[count].Width = 0; dataGridView1.Columns[count].Visible = false; //} } dataGridView1.Columns[0].Visible = true; dataGridView1.Columns[1].Visible = true; dataGridView1.Columns[2].Visible = true; dataGridView1.Columns[3].Visible = true; } catch (Exception) { this.DateFalse(); MessageBox.Show("文件格式错误!"); return; } //this.dataGridView1.Columns[1].Name.ToString() == "Date" && try { if (this.dataGridView1.Columns[1].Name.ToString() == "Date" && (this.dataGridView1.Columns[2].Name.ToString() == "Heure")) { this.label3.Text = Path.GetFileNameWithoutExtension(dlgOpenFile.FileName); this.label4.Text = Path.GetDirectoryName(dlgOpenFile.FileName); } else { this.DateFalse(); MessageBox.Show("文件非法", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } sr.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }