Click here to Skip to main content
15,881,882 members

Cannot populate a datagridview from text file

WarriorII asked:

Open original thread
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:

VB
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


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:

VB
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,

WarriorII
Tags: Visual Basic, Visual Studio

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900