如何将 .txt 文件放入数据源?

发布于 2024-11-30 11:12:25 字数 1508 浏览 3 评论 0原文

可能的重复:
将 .txt 文件放入 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

我对 DataSourceDataGridView 非常不熟悉,所以如果我可以获得一些关于它如何工作、需要发生什么、它看起来如何等方面的帮助,我们将不胜感激。 :)

谢谢。

Possible Duplicate:
Putting a .txt file into a DataGridView

When I click an open button I would like to choose a file and place it into a DataSource to futher be worked into a DataGridView.

Right now what I have looks like this:

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

Here is an example of a file I would be opening (space delimited):

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

I am very unfamiliar with DataSource and DataGridView so if I could get some help with how it works and what needs to happen, how it would look, etc. that would be greatly appreciated. :)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2024-12-07 11:12:25

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

例如(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);
        }
    }
}

You could split the lines and loop all rows/columns to generate the DataTable:

For example(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

This works just as well without LINQ (now also in 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);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文