分隔文本到DataTable [英] Delimited text to DataTable

查看:115
本文介绍了分隔文本到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个制表符分隔的文件,我想将其加载到DataGridView(DGV)中;为此,我正在使用以下代码:

I have a tab delimited file that I want to load in a DataGridView (DGV); for that, I'm using the following code:

DataTable dt = new DataTable();
using (FileStream stream = File.OpenRead("logs.txt"))
{
    using (StreamReader reader = new StreamReader(stream))
    {
        string line = reader.ReadLine();

        while (line != null)
        {
            string[] items = line.Split('\t');
            line = reader.ReadLine();

            if (dt.Columns.Count == 0)
            {
                for (int i = 0; i < items.Length; i++)
                {
                    dt.Columns.Add("Column " + i);
                }
            }
            dt.Rows.Add(items);
        }
        dataGridView1.DataSource = dt;
    }
}

问题是每行的列数并不总是相同,并且会产生错误"输入数组长于此表中的列".

The problem is the amount of columns per line is not always the same and that is producing the error "Input array is longer than the columns in this table".

文字示例:

x   xx  xxx xxxx    xxxxx
x   xx  xxx xxxx    xxxxx   xxxxxx  xxxxxxx
x   xx  xxx xxxx    xxxxx   xxxxxx  xxxxxxx xxxxxxxx    xxxxxxxxx

鉴于此问题,我如何才能将文本文件全部全部传递给DGV?

Given the problem, how can I make to pass all the text file to the DGV?

推荐答案

首先,您应该通过阅读有关异常的信息并弄清楚该异常在什么情况下抛出,来尝试理解该异常.

First of all, you should try to understand the exception by reading about it and figuring out on what situation this exception is thrown.

然后调试代码,了解为什么从您的代码中抛出此异常,并尝试了解如何解决此异常.

Then debug the code the understand why this exception is thrown from your code and try to understand how this can be fixed.

无论如何,回到您的代码.

Anyways, coming back to your code.

仅当dt.Columns.Count为零时,才将新列添加到表中.因此,将仅在文件的第一行添加列,因为有时不存在任何列.并且文件中第一行的值将成功添加到表中.

You are adding new columns to the table only when dt.Columns.Count is zero. So the columns will be added only for the first line from the file because there are no columns at point to time. And values of the first line from the file will be added to the table successfully.

此后,它不会添加新列,并且当您尝试向该行添加值时,您将获得异常,因为现在该行中的项目数与表中的列数不同.

After that it will not add new columns and when you try to add values to the row you will get exception because now the number of items in the line are different than the number of columns in the table.

因此,从逻辑上讲,您需要检查行中的项目数是否大于数据表中的当前列数.如果是,则在数据表中添加这些额外的列数.

So logically you need to check if the number of items in the line is greater than the current number of columns in the datatable. And if yes, then add those extra number of columns in the datatable.

并且也不要使用dt.Rows.Add,而是将这些值一一添加到行中.

And also instead of using dt.Rows.Add, add the values to the row one by one.

考虑以下代码.

DataTable dt = new DataTable();
using (FileStream stream = File.OpenRead("logs.txt"))
{
    using (StreamReader reader = new StreamReader(stream))
    {
        string line = reader.ReadLine();

        while (line != null)
        {
            string[] items = line.Split(',');

            // Check if the number of items in the line is 
            // greater than the current number of columns in the datatable.
            if(items.Length > dt.Columns.Count)
            {
                // Add new columns to the datatable.
                for (int i = dt.Columns.Count; i < items.Length; i++)
                {
                    dt.Columns.Add("Column " + i);
                }
            }

            // Create new row
            var newRow = dt.NewRow();

            // Loop thru the items and add them to the row one by one.
            for (var j = 0; j < items.Length; j++)
            {
                newRow[j] = items[j];
            }

            //Add row to the datatable.
            dt.Rows.Add(newRow);
            line = reader.ReadLine();
        }
        // Bind datatable to the gridview.
        dataGridView1.DataSource = dt;
    }
}

这应该可以帮助您解决问题.

This should help you resolve your issue.

这篇关于分隔文本到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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