【问题标题】:How to put a .txt file into a DataSource? [duplicate]如何将 .txt 文件放入 DataSource? [复制]
【发布时间】:2011-08-17 16:20:25
【问题描述】:

可能重复:
Putting a .txt file into a DataGridView

当我单击打开按钮时,我想选择一个文件并将其放入 DataSource 中,以便进一步放入 DataGridView 中。

我现在的样子是这样的:

OpenFileDialog openFile = new OpenFileDialog();

openFile.DefaultExt = "*.txt";
openFile.Filter = ".txt Files|*.txt";
openFile.RestoreDirectory = true;

try
{
    if (openFile.ShowDialog() == DialogResult.OK && openFile.FileName.Length > 0)
    {
        // Right now I am loading the file into a RichTextBox
        openFileRTB.LoadFile(openFile.FileName, RichTextBoxStreamType.PlainText);

        // What I would like to do is load it into a DataSource and then into a DataGridView.
        // So really I would like to remove the openFileRTB line of code and replace it.
        // That is where I need help :).
    }
}

catch (Exception)
{
    MessageBox.Show("There was not a specified file path to open.", "Path Not Found Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

这是我要打开的文件示例(空格分隔):

Title1 Title2 Title3 Title4 Title5 Title6
abc123 abc123-123-123 225.123 123.456 180 thing99
c123 somethingHERE 987.123 123.456 360 anotherThing1
abc124 somethingHERE225.123 123.456 0 thing99

我对@9​​87654326@ 和DataGridView 非常陌生,所以如果我能就它的工作原理、需要发生的事情、外观等方面获得帮助,我将不胜感激。 :)

谢谢。

【问题讨论】:

  • “进入数据源”?你到底是什么意思?
  • @Stakx:好吧,我想使用 IBindingSource,这样我就可以从 txt 文件中填充 DataGridView
  • @theNoobGuy,IBindingSource 不是容器。你不能把任何东西“放进”它。相反,它有自己的必须设置的DataSource 属性(意思是:您的数据必须转到其他地方,例如进入DataTable - 请参阅链接的问题)。所以IBindingSource 不是数据源,而是更类似于另一个数据源的适配器或装饰器。
  • @Stakx:感谢您的澄清。嗯

标签: c# text datagridview datasource


【解决方案1】:

您可以拆分行并循环所有行/列以生成数据表:

例如(VB.NET):

Dim separator = " "c
Dim fileName = OpenFileDialog1.FileName
Dim tbl As New DataTable(fileName)
Dim query = From line In IO.File.ReadAllLines(fileName)
            Let row = line.Split(separator)

If query.Any Then
   For Each col In query(0).row
         'build DataColumns from first line'
         tbl.Columns.Add(New DataColumn(col))
   Next
   If query.Count > 1 Then
       For rowIndex = 1 To query.Count - 1
           Dim newRow = tbl.NewRow
           For colIndex = 0 To query(rowIndex).row.Length - 1
               Dim colValue = query(rowIndex).row(colIndex)
               newRow(colIndex) = colValue
           Next
           tbl.Rows.Add(newRow)
       Next
   End If
End If

这在没有 LINQ 的情况下同样有效(现在也在 C# 中;)):

....
var rows = System.IO.File.ReadAllLines(fileName);
if (rows.Length != 0) {
    foreach (string headerCol in rows(0).Split(separator)) {
        tbl.Columns.Add(new DataColumn(headerCol));
    }
    if (rows.Length > 1) {
        for (rowIndex = 1; rowIndex < rows.Length; rowIndex++) {
            var newRow = tbl.NewRow();
            var cols = rows(rowIndex).Split(separator);
            for (colIndex = 0; colIndex < cols.Length; colIndex++) {
                newRow(colIndex) = cols(colIndex);
            }
            tbl.Rows.Add(newRow);
        }
    }
}

【讨论】:

  • 谢谢,有点帮助..虽然不是很熟悉 VB.net 哈哈
  • @theNoobGuy:我最近编辑了我的答案并提供了一个 C# 示例。
  • round(-0.49) 用于回答重复的问题。 ;)
  • @stakx:哦,我刚刚看到另一个问题不仅相似/重复,而且来自同一个 OP。感谢您的澄清,我已经在那里添加了我的答案,以便可以关闭它。
猜你喜欢
  • 2020-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 2011-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多