从文本文件导入数据并在数据网格中显示 [英] Import data from text file and display in datagrid

查看:18
本文介绍了从文本文件导入数据并在数据网格中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我已成功从文件中取出数据.问题是文件每次都可能不同,列数可能会有所不同.所以我不能为它使用预定义的类.我想为运行时创建一个类并在运行时添加属性并将列表显示到数据网格.我怎样才能完成这个任务?

解决方案

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

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 class for it.I wanted to create a class for run time and add the properties at run time and to display the list to data grid. How can I complete this task ?

解决方案

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

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,

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

这篇关于从文本文件导入数据并在数据网格中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆