partial .txt file to datagridview using vb.net
i have a user interface form that let's you upload a text file to a datagridview as follows
Sub Datagrid()
Dim sw = System.Diagnostics.Stopwatch.StartNew()
Using stream As System.IO.FileStream = System.IO.File.OpenRead(TextBox1.Text)
Using reader AsNew System.IO.StreamReader(stream)
Dim line AsString= reader.ReadLine()
While(line IsNot Nothing)
Dim columns = line.Split(";")
line = reader.ReadLine()
Dim index =Me.DataGridView1.Rows.Add()
Me.DataGridView1.Rows(index).SetValues(columns)
EndWhile
End Using
End Using
sw.Stop()
EndSub
Well, now my problem is that i don't want to put the full txt file in that datagridview, just from line N. Is it possible to do that? Like creating a querytabel and selecting a fixed value?
p.e., in line 5 there's always the text "Values:" . Can i select all the lines after that to put in the datagridview?
i googled everywhere but found nothing. and there's no "sample" code to give me a start .
thank you all !
Re: partial .txt file to datagridview using vb.net
If what you want in the data grid is everything from line 5 and after, then read the entire file into a lines() array and use a for:Next loop...
Code:
dim lines() as string = io.file.readAllLines(filePath)
For lineIndex as integer = 5 to lines.count-1
dim line as string = lines(lineIndex)
'import the line
Next
you should also turn on Option Strict and declare all of your variables.
Re: partial .txt file to datagridview using vb.net
oh nice, thank you!
thinking forward, how can i put lines 1 to 5 in datagridview1 (and 6 till the end in datagridview2) ?
Re: partial .txt file to datagridview using vb.net
Take a look at this example:
Code:
Dim n As Integer = 5
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
'Create a new datatable and add some columns
Dim dt As New DataTable
dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
'Add each line as a row to the datatable
For Each line As String In lines
dt.Rows.Add(line.Split(";"c))
Next
'Set the datasource of the datagridview
MyDataGridView.DataSource = dt
Edit - I just noticed that you want to skip some lines too. You'd adjust the lines variable to:
Code:
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Take(n) 'Gets every line up to a certain line count
If you wanted to get lines 2 - 5 then you'd do:
Code:
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(1).Take(4) 'Gets every line between two values
Re: partial .txt file to datagridview using vb.net
Quote:
Originally Posted by
dday9
Take a look at this example:
Code:
Dim n As Integer = 5
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
'Create a new datatable and add some columns
Dim dt As New DataTable
dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
'Add each line as a row to the datatable
For Each line As String In lines
dt.Rows.Add(line.Split(";"c))
Next
'Set the datasource of the datagridview
MyDataGridView.DataSource = dt
Edit - I just noticed that you want to skip some lines too. You'd adjust the lines variable to:
Code:
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Take(n) 'Gets every line up to a certain line count
If you wanted to get lines 2 - 5 then you'd do:
Code:
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(1).Take(4) 'Gets every line up to a certain line count
That answers my question ! :) thank you guys ! cumps
Re: partial .txt file to datagridview using vb.net
I noticed something in my code, change this line:
Code:
dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
To this:
Code:
dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
The adjustment will name the columns: Column1, Column2, etc. rather than naming them the respective values in the first row.
Re: partial .txt file to datagridview using vb.net
Quote:
Originally Posted by
dday9
I noticed something in my code, change this line:
Code:
dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
To this:
Code:
dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
The adjustment will name the columns: Column1, Column2, etc. rather than naming them the respective values in the first row.
the way u are doing that, each line creates a new column instead of a new row.
i'm trying to adapt the code now .
edit2: i can't make it to write in the cells, not in the headers ( i already have the headers) and when i changed the code, i added the exact number of rows, but without any values in the cells..
Re: partial .txt file to datagridview using vb.net
If you have the column headers already then just skip that line but keep the rest of the code.
Re: partial .txt file to datagridview using vb.net
Quote:
Originally Posted by
dday9
If you have the column headers already then just skip that line but keep the rest of the code.
already did that, of course. it still gives me an error..
Dim n As Integer = 5
Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines(TextBox1.Text).Skip(n)
Dim dt As New DataTable
' dt.Columns.AddRange((From column As String In lines.First.Split(";"c) Select New DataColumn(column)).ToArray())
For Each line As String In lines
dt.Rows.Add() '(line.Split(";"c))
Next
DataGridView2.DataSource = dt
this is the only way to make it "kinda" work. it adds the rows, but adds no values. if i put the full code it gives me an error in the split method..
Re: partial .txt file to datagridview using vb.net
How does each line separate it's cells? I thought that the layout of the text file was like this:
Code:
1;2;3;4;5
6;7;8;9;10
11;12;13;14;15
Is it in a different format?
Re: partial .txt file to datagridview using vb.net
Quote:
Originally Posted by
dday9
How does each line separate it's cells? I thought that the layout of the text file was like this:
Code:
1;2;3;4;5
6;7;8;9;10
11;12;13;14;15
Is it in a different format?
nope. just like that !
edit: i'll try to translate_
System.ArgumentException: The input matrix is longer than the number of columns in this table. ( and it isn't!! format: "1;13:01:22;3,8;OK" )
Re: partial .txt file to datagridview using vb.net
When I test my code on declared string with that content I get the desired results. This is what I tested on a console application:
Code:
Imports System
Imports System.Collections.Generic
Imports System.Data
Imports System.Linq
Imports System.Xml
Public Module Module1
Public Sub Main()
Dim n As Integer = 5
'Dim lines As IEnumerable(Of String) = IO.File.ReadAllLines("my_file.txt").Skip(n) 'Gets every line after a certain line count
Dim lines() As String = {"1;2;3;4;5", "6;7;8;9;10", "11;12;13;14;15"}
'Create a new datatable and add some columns
Dim dt As New DataTable
dt.Columns.AddRange((From columnIndex As Integer In Enumerable.Range(1, lines.First.Split(";"c).Count) Select New DataColumn("Column" & columnIndex.ToString())).ToArray())
'Add each line as a row to the datatable
For Each line As String In lines
dt.Rows.Add(line.Split(";"c))
Next
'Set the datasource of the datagridview
'MyDataGridView.DataSource = dt
For Each row As DataRow In dt.Rows
Console.WriteLine(String.Join(" | ", (From c As DataColumn In dt.Columns Select row.Item(c)).ToArray()))
Next
End Sub
End Module
Fiddle: https://dotnetfiddle.net/1khNDC
Re: partial .txt file to datagridview using vb.net
i did that now and it worked. It created new columns and added the values like expected.
but then i removed the code to add new columns and it didn't work ( with the same error)
Re: partial .txt file to datagridview using vb.net
Wait, are you trying to add columns to the DataGridView manually like this:
Code:
MyDatagridView.Columns.Add(....
If so then you can't do that with my code. The reason for this is because we're setting the DataSource of the DataGridView:
Code:
MyDataGridView.DataSource = dt
Re: partial .txt file to datagridview using vb.net
Quote:
Originally Posted by
dday9
Wait, are you trying to add columns to the DataGridView manually like this:
Code:
MyDatagridView.Columns.Add(....
If so then you can't do that with my code. The reason for this is because we're setting the DataSource of the DataGridView:
Code:
MyDataGridView.DataSource = dt
is there any other way to change the column name? lol
anyway, u solved my problem. thank you! i really don't care about the headers :) cumps
Re: partial .txt file to datagridview using vb.net
Yeah, if you want the column names to be different then change them in the DataTable rather than in the DataGridView. Or if you want column names to remain the same but the header text to be different then change the column's caption in the DataTable.