如何使用VB.NET读取文本文件数据并在datagridview中显示 [英] How to read text file data and display in datagridview with VB.NET

查看:407
本文介绍了如何使用VB.NET读取文本文件数据并在datagridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我使用数据网格显示已保存到纺织品中的信息(库存相关信息)。我使用了下面的代码,显示的错误是索引超出了数组的范围。有没有人有任何建议?

我们将不胜感激。



我的尝试:



In my project I'm using a data grid to display the information that has been saved into the textile (Stock related information). I have used the code below and the error that is showing is "Index was outside the bounds of the array". Has anyone got any suggestions?
It would be appreciated.

What I have tried:

Dim fname As String = "C:\Users\Windows 7 User\Desktop\Stock.txt"
        Dim colsexpected As Integer = 8
        Dim thereader As New StreamReader(fname, Encoding.Default)
        Dim sline As String
        Do
            sline = thereader.ReadLine
            If sline Is Nothing Then Exit Do
            Dim words() As String = sline.Split(",")
            DataGridView1.Rows.Add("")
            For ix As Integer = 0 To 7
                DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(ix).Value = words(ix)

            Next

        Loop

        thereader.Close()

推荐答案

读取分隔文本文件的最佳方法是使用 OleDb [ ^ ]。

您可以在此处找到示例代码:读取文本文件特定列 [ ^ ]



我建议你也阅读:

关于文本文件的这个ADO [ ^ ]

Schema.ini文件(文本文件驱动程序)| Microsoft Docs [ ^ ]

如何:向文本文件数据源添加模式定义 [ ^ ]

使用OleDb导入文本文件(选项卡,CSV,自定义) [ ^ ]



祝你好运!
The best way to read delimited text file is to use OleDb[^].
An example code you'll find here: Read Text File Specific Columns[^]

I'd recommend to read also:
Much ADO About Text Files[^]
Schema.ini File (Text File Driver) | Microsoft Docs[^]
How to: Add a Schema Definition to a Text File Data Source[^]
Using OleDb to Import Text Files (tab, CSV, custom)[^]

Good luck!


这篇关于如何使用VB.NET读取文本文件数据并在datagridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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