无法从文本文件填充datagridview

问题描述:

大家好,

我一直在网上寻找一个简单的例子来实现上述目标.我是编程的初学者,所以请使其尽可能简单.我正在使用Visual Basic2010.

我有一个这种格式的文本文件:

Hello all,

I have been looking all over the net for a good easy example to achieve the above. I''m a beginner in programming, so please make it as simple as possible. I''m working with Visual Basic 2010.

I have a text file in this format:

0110000105c179a7 _____,.__
0110000105b4f67a (( *  
011000010597d255 _-StraNn1K-_
0110000103f169d4 ..
0110000105b8a73f [GER]Skilljump



无标题...左侧的数据固定长度为16个字母数字字符,右侧的变量各列用单个空格分隔.我想将文件读入数据网格,以便用户可以浏览它并根据需要添加和/或删除行,然后以相同格式重新保存文本文件.之所以需要这样,是因为该文件属于另一个程序,这就是它们所使用的,我只想自动化处理文件的过程,而不必手动操作文本文件(记事本/写字板等).

我尝试了以下代码:



No headings... The data on the left has a fixed length of 16 alphanumeric characters, the right side variable the columns are separated by a single space. I would like to read the file into a datagrid so that the user can look through it and add and/or delete lines as needed then re-save the text file in the same format. It needs to be this way because the file belongs to another program and that is what they use, I merely want to automate the process of working witht the file and avoid having to manipulate a text file by hand (notepad/wordpad, etc).

I have tried the following code:

Dim lines() As String = IO.File.ReadAllLines("C:\permanent.ban")
        Dim dt As New DataTable

        For x As Integer = 1 To lines(0).Split(CChar(vbTab)).Length
            dt.Columns.Add("Column" & x.ToString)

        Next
        For x As Integer = 0 To lines.GetUpperBound(0)
            Dim dr As DataRow = dt.NewRow
            dr.ItemArray = lines(x).Split(CChar(vbTab))
            dt.Rows.Add(dr)
        Next

        DataGridView1.DataSource = dt



它多少有些用,但是这会产生datagridview并将所有数据都放在一列中,并且还在每条记录之后创建一个空白行.我尝试添加:



It somewhat works, but this produces the datagridview with all the data in one column and also creates a blank row after each record. I have tried to add:

dt.Columns.Add("Column2" & x.ToString)



但是它所做的只是出于某些奇怪的原因而添加了第二列,其标题为"Column21".

您能提供的任何帮助将不胜感激.

再次感谢,

WarriorII



But all it does is add a second column with a heading of "Column21" fo some strange reason.

Any help you can provide would be GREATLY appreciated.

Thanks again,

WarriorII

检查此代码
check this code
Private Sub fillgrid()
        dataGridView1.Columns.Add("", "column1")
        dataGridView1.Columns.Add("", "column2")
        Dim strlines() As String = System.IO.File.ReadAllLines("C:\permanent.ban")
        Dim i As Integer
        For i = 0 To strlines.Length - 1 Step i + 1
            dataGridView1.Rows.Add()
            DataGridView1(0, i).Value = strlines(i).Split(" ")(0)

            DataGridView1(1, i).Value = strlines(i).Split(" ")(1)
        Next

    End Sub


这是一种方法,您可以使用.请注意,在将行添加到网格之前,需要确保创建列.

Here is an approach that you could use. Please be aware that you would need to make sure that you create the columns before you being to add the rows to the grid.

  Dim data As String = "1,2,3,4,5,6,7"
  Dim values As String() = data.Split(",")

  DataGridView1.Rows.Add(New String() {values(0), values(1), values(2), values(3), values(4), values(5), values(6)})
<pre>