C# 加载文本文件并填充到gridview中

C# 加载文本文件并填充到gridview中,c#,gridview,C#,Gridview,我有一个代码,允许用户选择文件并将文本文件中的数据加载到datagridview private void cmdload_Click(object sender, EventArgs e) { DataTable table = new DataTable(); table.Columns.Add("Point"); table.Columns.Add("X"); table.Columns.Add("Y");

我有一个代码,允许用户选择文件并将文本文件中的数据加载到datagridview

 private void cmdload_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Point");
        table.Columns.Add("X");
        table.Columns.Add("Y");
        table.Columns.Add("Z");
        table.Columns.Add("R");
        table.Columns.Add("A");
        table.Columns.Add("B");
        table.Columns.Add("C");

        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "\\Yamaha";
        openFileDialog1.Filter = "Data Files (*.PNT)|*.PNT";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
         {
             try
             {
                 if ((myStream = openFileDialog1.OpenFile()) != null)
                 {
                     using (myStream)
                     {
                         string filename = openFileDialog1.FileName;
                         using (var reader = File.OpenText(@filename))
                         {
                             string line;
                             while ((line = reader.ReadLine()) != null)
                             {
                                 string[] parts = line.Split(' ');
                                 table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
                             }
                             dataGridView1.DataSource = table;
                         }
                     }
                 }
             }
编辑:

我已经按照建议编辑了代码,但没有显示值。请告知

private void cmdload_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Point");
        table.Columns.Add("X");
        table.Columns.Add("Y");
        table.Columns.Add("Z");
        table.Columns.Add("R");
        table.Columns.Add("A");
        table.Columns.Add("B");
        table.Columns.Add("C");
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "C:\\"; 
        openFileDialog1.Filter = "Data Files (*.PNT)|*.PNT";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        string filename = openFileDialog1.FileName;
                        using (var reader = File.OpenText(filename)) 
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                string[] parts = line.Split(' ');
                                table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
                            }
                            dataGridView1.DataSource = table;
                        }
                    }
                }
            }

            catch (Exception ex) // you need to add the catch block if yo are using try block
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }

        }
    }
文本文件如下所示:

我希望在用户加载文件时将数据文件粘贴到gridview中。

现在我不知道为什么我的代码不起作用。谁能给我一个建议吗?

请看一下:

    private void cmdload_Click(object sender, EventArgs e)
    {
        DataTable table = new DataTable();
        table.Columns.Add("Point");
        table.Columns.Add("X");
        table.Columns.Add("Y");
        table.Columns.Add("Z");
        table.Columns.Add("R");
        table.Columns.Add("A");
        table.Columns.Add("B");
        table.Columns.Add("C");
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "C:\\"; // your directory is also not defined properly
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";// have a look to filter as well
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = true;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        string filename = openFileDialog1.FileName;
                        using (var reader = File.OpenText(filename)) // you need not to use '@filename' instead use just 'filename'
                        {
                            string line;
                            while ((line = reader.ReadLine()) != null)
                            {
                                string[] parts = line.Split(' ');
                                table.Rows.Add(parts[0], parts[1], parts[2], parts[3], parts[4], parts[5], parts[6], parts[7]);
                            }
                            dataGridView1.DataSource = table;
                        }
                    }
                }
            }

            catch (Exception ex) // you need to add the catch block if yo are using try block
            {
            }
        }

希望有帮助。

定义“不起作用”。运行时崩溃?添加了错误的列?没有数据填充?填充了错误的数据?把冰箱里的啤酒都喝光了?M@MatthewWatson没有错误,只是该值不显示在gridview上。有什么想法吗?我已经像上面那样编辑了代码,但是仍然没有显示在gridview上。请告诉我上面的代码应该可以运行,试着调试一下,看看有什么问题(或者更新你的最新代码)