C# 数据表的分隔文本

C# 数据表的分隔文本,c#,.net,parsing,datagridview,tab-delimited,C#,.net,Parsing,Datagridview,Tab Delimited,我有一个制表符分隔的文件,我想加载到DataGridView(DGV)中;为此,我使用以下代码: DataTable dt = new DataTable(); using (FileStream stream = File.OpenRead("logs.txt")) { using (StreamReader reader = new StreamReader(stream)) { string line = reader.ReadLine();

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

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;
    }
}

考虑到这个问题,我如何将文本文件传递给DGV?

首先,您应该通过阅读有关异常的内容并了解该异常是在什么情况下引发的,从而尝试理解该异常

然后调试代码,了解为什么会从代码中抛出此异常,并尝试了解如何修复此异常

无论如何,回到你的代码

只有当
dt.columns.Count
为零时,才向表中添加新列。因此,将只为文件的第一行添加列,因为此时没有列。文件中第一行的值将成功添加到表中

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

因此,从逻辑上讲,您需要检查行中的项数是否大于datatable中当前的列数。如果是,则在datatable中添加额外数量的列

也不要使用
dt.Rows.Add
,而是将值逐个添加到行中

考虑以下代码

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;
    }
}
DataTable dt=newdatatable();
使用(FileStream stream=File.OpenRead(“logs.txt”))
{
使用(StreamReader=新StreamReader(stream))
{
字符串行=reader.ReadLine();
while(行!=null)
{
string[]items=line.Split(',');
//检查行中的项目数是否为
//大于数据表中当前的列数。
if(items.Length>dt.Columns.Count)
{
//向datatable添加新列。
for(int i=dt.Columns.Count;i

这将有助于您解决问题。

有什么方法可以计算出文件的最大列数?是否有任何标准或规则可以确保文件中只包含特定数量的列?@ChetanRanpariya遗憾的是,没有,这就是让这有点复杂的原因:(
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;
    }
}