Click here to Skip to main content
12,820,118 members (24,966 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB Visual-Studio
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

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)

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

        DataGridView1.DataSource = dt

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)

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,

Posted 9-Sep-12 11:57am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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(0, i).Value = strlines(i).Split(" ")(0)

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

    End Sub
WarriorII 10-Sep-12 19:42pm
Thanks much for your reply Menon, For some reason I'm getting an error: "An unhandled exception of type 'System.IndexOutOfRangeException' ocurred. Index was outside the bounds of the array." Weird thing is that I tried it first at work on another PC and came home happy to let you know it worked, but I decided to try it one more time and I got this error... It is pointing to the "DataGridView1(1, i).Value = strlines(i).Split(" ")(1)" Line... Also, what would I need to add in order to save this back to the text file? Thanks again!!! WarriorII
WarriorII 12-Sep-12 0:47am
Hi Menon, just wondering if you've had achance to help me out with this, as I'm still stuck :(
Thanks much...
WarriorII 13-Sep-12 1:43am
Hi Menon, or anyone that can help, hope you are avaible to help me with this, still cannot get it working right... I replaced the "DatagridView1(,0..." with "DataGridView1(0, i).Value = strlines(i).Split(New Char() {" "c}, 2)(0)" & "DataGridView1(1, i).Value = strlines(i).Split(New Char() {" "c}, 2)(1)" respectively. Becausae I noticed that some of the entries in column 2 had spaces, and they were being ignored. I also noticed that if the text file is less than about 20 rows it works fine, but any more lines than that it throws the same "IndexOutOfRange" error. Also, as stated above I need to know how to save this data back to its text file.

Would greatly appreciate anyone's help on this. In advance THANK YOU!
WarriorII 14-Sep-12 19:43pm
Can I please please have some help on this...

I have noticed that if I copy and paste the data line by line into a new file, it works, but I cannot understand why it does this, I need it to work as is because its coming from another program, dunno, running out of ideas...

Please help...

Thanks in advance...
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100