wpf 从文本文件导入数据并在数据网格中显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26753914/
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-09-13 12:40:33  来源:igfitidea点击:

Import data from text file and display in datagrid

c#.netwpfdatagrid

提问by Aron

I want to import data from a text file and to display it in a data grid. The text file is delimited.The first row contains column headers and the rest contains the data for respective columns.

我想从文本文件中导入数据并将其显示在数据网格中。文本文件被分隔。第一行包含列标题,其余包含各列的数据。

There is column delimiters and row delimiters are present in text file.I want to display the data in a data grid in which header will be the column name and all the data will be display under each column.

文本文件中存在列分隔符和行分隔符。我想在数据网格中显示数据,其中标题将是列名,所有数据将显示在每列下。

I have successfully taken the data from the file. The problem is the file may different for each time and the number of columns may vary. So I can not use a predefined classfor it.I wanted to create a class for run time and add the properties at run timeand to display the list to data grid. How can I complete this task ?

我已经成功地从文件中获取了数据。问题是文件每次都可能不同,列数可能会有所不同。所以我can not use a predefined class为它。我想create a class for run time and add the properties at run time并将列表显示到数据网格。我怎样才能完成这个任务?

回答by Deepak Sharma

why you want to create the class?? you can use the below code as well.. it will dynamically make the DataTable

为什么要创建类?你也可以使用下面的代码..它会动态地制作数据表

check here as well..

也在这里检查..

public class Helper
{
    public static DataTable DataTableFromTextFile(string location, char delimiter = ',')
    {
        DataTable result;

        string[] LineArray = File.ReadAllLines(location);

        result = FormDataTable(LineArray, delimiter);

        return result;
    }

    private static DataTable FormDataTable(string[] LineArray, char delimiter)
    {
        DataTable dt = new DataTable();

        AddColumnToTable(LineArray, delimiter, ref dt);

        AddRowToTable(LineArray, delimiter, ref dt);

        return dt;
    }

    private static void AddRowToTable(string[] valueCollection, char delimiter, ref DataTable dt)
    {

        for (int i = 1; i < valueCollection.Length; i++)
        {
            string[] values = valueCollection[i].Split(delimiter);
            DataRow dr = dt.NewRow();
            for (int j = 0; j < values.Length; j++)
            {
                dr[j] = values[j];
            }
            dt.Rows.Add(dr);
        }
    }

    private static void AddColumnToTable(string[] columnCollection, char delimiter, ref DataTable dt)
    {
        string[] columns = columnCollection[0].Split(delimiter);
        foreach (string columnName in columns)
        {
            DataColumn dc = new DataColumn(columnName, typeof(string));
            dt.Columns.Add(dc);
        }
    }

}

now to show the this DataTable to your grid view you just need to call as below

现在要将这个 DataTable 显示到您的网格视图中,您只需要调用如下

dataGridView1.DataSource = Helper.DataTableFromTextFile("Testing.txt");

for text file like -

对于文本文件,如 -

fname, sname, age
deepak, sharma, 23
Gaurav, sharma, 32
Alok, Kumar, 33

as you have not specified the delimiter char it will use ,by default else you have to specified if have any other like

由于您没有指定分隔符字符,它将,默认使用否则您必须指定是否有任何其他类似

dataGridView1.DataSource = Helper.DataTableFromTextFile("Testing.txt", '|');

for text file like -

对于文本文件,如 -

fname| sname| age
deepak| sharma| 23
Gaurav| sharma| 32
Alok| Kumar| 33

it works like charm,

它就像魅力一样,

enter image description here

在此处输入图片说明

http://mytactics.blogspot.com/2014/11/show-delimiter-text-file-data-to.html

http://mytactics.blogspot.com/2014/11/show-delimiter-text-file-data-to.html