我想实现这样一个功能,界面上有两个DataGridView控件,点击其中一个DataGridView中的某记录,即可从数据库中读取相应的文本文件中的内容(如下所示),并显示在另一个DataGridView控件中,请大家帮忙想一些具体的代码应咋写,我是个新手,感觉这不是个很难的问题,可是就是搞不定,快愁死了!文本文件的内容如下:EportSoft Tube ... L                  α                  β                   R (这一行不显示)
              22.000
              13.809               0.000              55.585              44.000
             143.128               0.000               0.000               0.000
#eof(这一行也不现实)或者EportSoft Tube ... (这一行不显示)
75
0.000 0.000 90.000 75.000
108.048 90.000 28.809 150.000
48.400 0.000 0.000 0.000 
#eof(这一行不显示)

解决方案 »

  1.   

    可以参考下这个,然后差不多就是字符串处理的问题了
    using System;
    using System.IO;
    class Test
    {
        public static void Main()
        {
            string path = @"c:\temp\MyTest.txt";        // This text is added only once to the file.
            if (!File.Exists(path))
            {
                // Create a file to write to.
                string[] createText = { "Hello", "And", "Welcome" };
                File.WriteAllLines(path, createText);
            }        // This text is always added, making the file longer over time
            // if it is not deleted.
            string appendText = "This is extra text" + Environment.NewLine;
            File.AppendAllText(path, appendText);        // Open the file to read from.
            string[] readText = File.ReadAllLines(path);
            foreach (string s in readText)
            {
                Console.WriteLine(s);
            }
        }
    }
      

  2.   


    嗯,我试过了,在记事本中写入文字,那么我帮楼主提一个思路,既然二楼已经给出了读和写文本文件中的方法,楼主就可以就只用读取字段,然后添加到DataGridView里面,添加到DataSet["Tabel"]里面就行了.
      

  3.   

    我尝试了以下好几种方法,但是结果不是出错就是现实不正确,大家帮忙看看错误出在哪里?谢谢了!
    第一种如下:
    FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
                     StreamReader sr = new StreamReader(fs);                    
                     string columnInfo = sr.ReadLine();
                        char[] seperator = new char[] { ' ' };
    string[] columns = columnInfo.Trim().Split)     //错误在这里 未将对象引用设置到对象的实例
    (seperator,StringSplitOptions.RemoveEmptyEntries );                   this.dataGridView_导管实样坐标信息.Rows.Add(columns);
                       sr.Close();  但是这个显示出错了。  第二种方法:
    using (TextFieldParser myReader = new TextFieldParser(filePath))
                      {
                         string[] currentRow;
                         this.dataGridView_导管实样坐标信息.Rows.Clear();
                           myReader.TextFieldType = FieldType.Delimited;
                      myReader.Delimiters = new String[] { " " };//出错,分隔符不能为空,可是我改为“,”后显示出来的是将一行的数据全部显示在DataGridview的一个单元格中
                          while (!myReader.EndOfData)
                        {
                               try
                             {                              currentRow = myReader.ReadFields();                               this.dataGridView_导管实样坐标信息.Rows.Add(str);
                                                        
                              }
                           catch (MalformedLineException ex)
                            {
                              MessageBox.Show("行 " + ex.Message + " 是无效的。略过。");
                             }
                    }
               }出错:分隔符不能为空,可是将字段两头不需要的字符也显示了!大家快帮忙想想办法!
      

  4.   

    顶[align=center]****************************************************************
                看帖一定要回的,分也一定要接的。^_^
    ****************************************************************[/align]
      

  5.   

    谢谢大家的思路提示,思路我是明白了,可是代码怎么来写呢?字符串处理的代码有怎么写呢?我怎么写才能不显示文本中的第一行和最后一行,将文本中的中间不确定的几行读取显示到datagridview中呢?
      

  6.   

     '读取文本文件
        Private Sub Read_File(ByVal filename As String)
            Dim dTable As New DataTable
            Dim Reader As New StreamReader(filename, System.Text.Encoding.Default) '参数分别为带路径的文件名和字符编码
            Dim sLine As String = Reader.ReadLine()
            Dim sVals(), sVal As String
            Dim i As Integer
            i = 0
            While Not (sLine Is Nothing)
                '文件中使用tab键作为分隔符
                sVals = Split(sLine, ControlChars.Tab)
                If i = 0 Then
                    For Each sVal In sVals
                        dTable.Columns.Add(sVal)
                    Next
                Else
                    dTable.Rows.Add(sVals)
                End If
                sLine = Reader.ReadLine()
                i = i + 1
            End While
            DataGridView1.DataSource = dTable