如何使用 C#.. 在 datagridview 中读取/加载文本 (*.txt) 文件值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19582570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:21:52  来源:igfitidea点击:

How to read /load text (*.txt) file values in datagridview using C# ..?

c#winformsdatagridview

提问by Vigna

can anyone help me..?

谁能帮我..?

Here, i need to read/load text (*.txt) file values in my datagridview. this is that sample text file, which i need to load.

在这里,我需要在我的 datagridview 中读取/加载文本 (*.txt) 文件值。这是我需要加载的示例文本文件。

 S.NO   Data1  Data2    Data3   Data4   Data5  Data6   Data7   Data8   Data9   Data10

 1      8.3     2       9.1     3       7.5     1       25      1.5     22      1.7 
 2      5.6     4       8.2     6       8.6     3       26      2.5     23      2.3 
 3      8.7     6       7.3     9       9.3     5       28      3.5     26      3.7 
 4      2.9     8       6.4     12      4.9     7       12      4.5     24      4.3 
 5      4.6    10       5.5     15      5.7     9       25      5.5     25      5.3

can anyone present how to load these text file values to my datagridview cells, those heading like data1,data2,.....data10 must load in column header and remaining row values should load in seperate cells of datagridview according to their column header.

任何人都可以介绍如何将这些文本文件值加载到我的 datagridview 单元格中,像 data1、data2、.....

Thanks in advance.

提前致谢。

回答by Alex Filipovici

One way to do it is:

一种方法是:

var lines = File.ReadAllLines("input.txt");
if (lines.Count() > 0)
{
    foreach (var columnName in lines.FirstOrDefault()
        .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
    {
        dataGridView1.Columns.Add(columnName, columnName);
    }
    foreach (var cellValues in lines.Skip(1))
    {
        var cellArray = cellValues
            .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (cellArray.Length == dataGridView1.Columns.Count)
            dataGridView1.Rows.Add(cellArray);
    }
}

Of course, this works for the sample input file that you provided. Any variation to that format would require further validation and an increase in code complexity.

当然,这适用于您提供的示例输入文件。该格式的任何变化都需要进一步验证并增加代码复杂性。

回答by Ganesh Jadhav

Try this..

尝试这个..

System.IO.StreamReader file = new System.IO.StreamReader("yourfile.txt");
string[] columnnames = file.ReadLine().Split(' ');
DataTable dt = new DataTable();
foreach (string c in columnnames)
{
    dt.Columns.Add(c);
}
string newline;
while ((newline = file.ReadLine()) != null)
{
    DataRow dr = dt.NewRow();
    string[] values = newline.Split(' ');
    for (int i = 0; i < values.Length; i++)
    {
        dr[i] = values[i];
    }
    dt.Rows.Add(dr);
}
file.Close();
dataGridView1.DataSource = dt;

And don't be discouraged, but this is not the proper way to ask a question out here on SO. Please get yourself familiar first.

不要气馁,但这不是在 SO 上提出问题的正确方法。请先熟悉一下。

回答by DDark Burning Chris

        this.Size = new Size(750, 450);
        data.Size = new Size(700, 200);
        data.Location = new Point(5, 40);

        string[] raw_text = System.IO.File.ReadAllLines("Etudiant.txt");
        string[] data_col = null;

        int x = 0;

        foreach (string text_line in raw_text) {
            //MessageBox.Show(text_line);
            data_col = text_line.Split('|');

            if (x == 0){
                //header
                for (int i=0; i <= data_col.Length - 1; i++) {
                    table.Columns.Add(data_col[i]);
                }
                x++;
            }
            else {
                //data
                table.Rows.Add(data_col);
            }
        }

        data.DataSource = table;
        this.Controls.Add(data);