Click here to Skip to main content
16,021,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm developing a small application using VB.NET in a company that I'm currently working with. I already did the browsing and reading of all lines in the text file, but may problem is the displaying of specific content to a datagridview.

I have 2 datagrid columns named BL No. and No. of Packages.

Text file content and format is something like this:

BL101=SM1110
BL202=20
BL303=MANILA
BL101=SM1120
BL202=50
BL303=CEBU
ETC....


Now, when I click the load button of may application, all BL101 value will display in BL No. column and all BL202 value will display in No. of Packages column like this.

BL No.                  No. of Package
SM1110                       20
SM1120                       50


I've tried a lot of way but unfortunately still can't get what I want, so please help me.

Looking forward for your quick response and thank you in advance.

What I have tried:

Private Sub btn_load_Click(sender As Object, e As EventArgs) Handles btn_load.Click
       Dim lines = (From line In IO.File.ReadAllLines(txt_edifile.Text)
                    Select line.Split(CChar(vbTab))).ToArray
       For x As Integer = 0 To lines(0).GetUpperBound(0)
           DataGridView1.Columns.Add(lines(0)(x), lines(0)(x))
       Next
       For x As Integer = 0 To lines.GetUpperBound(0)
           DataGridView1.Rows.Add(lines(x))
       Next

   End Sub
Posted
Updated 3-Apr-18 0:43am
v3

You need a collection of Pairs of strings, see Pair Class (System.Web.UI)[^]. Create a new pair each time you get a BL101 record and add the value. When you get the corresponding BL202 record, then add its value to the current pair, and add that pair to the list. When you have processed all the data you can use the list as the binding source for your data grid.
 
Share this answer
 
You should work on data instead of its string representation!!!. It's called OOP[^].

I'd suggest to create a class:
VB.NET
Public Class MyData
	Private sName As String = ""
	Private iCount As Integer = 0
	Private sDescription As String = ""
	
	Public Sub New(ByVal portion As String())
		sName = portion(0).Split("=")(1)
		iCount = Int32.Parse(portion(1).Split("=")(1))
		sDescription = portion(2).Split("=")(1)
	End Sub
	
	Public Property Name As String
		Get
			Return sName
		End Get
		Set(value As String)
			sName = value
		End Set
	End Property

	Public Property Count As Integer
		Get
			Return iCount
		End Get
		Set(value As Integer)
			iCount = value
		End Set
	End Property
	
	Public Property Description As String
		Get
			Return sDescription
		End Get
		Set(value As String)
			sDescription = value
		End Set
	End Property

End Class


Then...
VB.NET
Dim lines As String() = File.ReadAllLines("FullFileName.txt")
Dim i As Integer= 0
Dim k As Integer = lines.Count()
Dim data As List(Of MyData) = New List(Of MyData)

Do While i < k
    data.Add(New MyData(lines.Skip(i).Take(3).ToArray()))
    i+=3
Loop
    'HERE!
    DataGridView1.DataSource = data


Result:
Name   Count Description
SM1110 20    MANILA 
SM1120 50    CEBU 


For further details, please see:
Creating Classes in Visual Basic .NET[^]
DataGridView.DataSource Property (System.Windows.Forms)[^]
 
Share this answer
 
You need to look rather more closely at your data, and how it is structured: you can;t split on a TAB when your data is separated by "=", and you don't take any account that a row of your data is split across three lines:
BL101=SM1110
BL202=20
BL303=MANILA

BL101=SM1120
BL202=50
BL303=CEBU
What I would do is use Split to break each line on '=', then use a switch to decide which data it is: BL101, BL202, or BL303. Then store the info for first two, and use the third to add both items to the DGV as a row.
 
Share this answer
 

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



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