-
Feb 11th, 2016, 11:45 AM
#1
Thread Starter
Member
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 !
-
Feb 11th, 2016, 12:44 PM
#2
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.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Feb 11th, 2016, 12:55 PM
#3
Thread Starter
Member
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) ?
-
Feb 11th, 2016, 01:02 PM
#4
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
Last edited by dday9; Feb 11th, 2016 at 01:10 PM.
-
Feb 11th, 2016, 01:10 PM
#5
Thread Starter
Member
Re: partial .txt file to datagridview using vb.net
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
-
Feb 11th, 2016, 01:21 PM
#6
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.
Last edited by dday9; Feb 11th, 2016 at 01:27 PM.
Reason: changed column to row
-
Feb 12th, 2016, 05:54 AM
#7
Thread Starter
Member
Re: partial .txt file to datagridview using vb.net
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..
Last edited by iDio; Feb 12th, 2016 at 07:24 AM.
-
Feb 12th, 2016, 09:16 AM
#8
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.
-
Feb 12th, 2016, 09:41 AM
#9
Thread Starter
Member
Re: partial .txt file to datagridview using vb.net
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..
-
Feb 12th, 2016, 09:47 AM
#10
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?
-
Feb 12th, 2016, 09:52 AM
#11
Thread Starter
Member
Re: partial .txt file to datagridview using vb.net
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" )
Last edited by iDio; Feb 12th, 2016 at 09:58 AM.
-
Feb 12th, 2016, 10:01 AM
#12
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
-
Feb 12th, 2016, 10:08 AM
#13
Thread Starter
Member
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)
-
Feb 12th, 2016, 10:30 AM
#14
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
-
Feb 12th, 2016, 10:38 AM
#15
Thread Starter
Member
Re: partial .txt file to datagridview using vb.net
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
-
Feb 12th, 2016, 10:49 AM
#16
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|