Hi
Here is another version, closer to what you want. It is still very much dependant on using the correct 'rules' as mentioned above. This version is using consecutive strings of the " " character to eliminate the surplus data on each line read in from the file.
There may be other considerations, but give this version a try and see if it can fit in with your needs. At this point, we are talking ONLY of these code examples being tested as stand alone tests - once established as working as expected then a more usable version may be developed (such as a Function to return data formatted suitably for display.
Anyway, Image and code below. (ignore previous example for now - I would have tried to Edit or Delete it but the deficiencies of this forum make it not worth the effort as it would likely cause issues and headaches and trouble and pain in the neck and a lot of swearing!
' Form1 with 4 ListBoxes (1,2,3 and 4)
' and a Button1
Option Strict On
Option Explicit On
Public Class Form1
Dim dt As New DataTable("F")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Size = New Size(600, 400)
With dt
.Columns.Add("Village Code", GetType(String))
.Columns.Add("Grower Code", GetType(String))
.Columns.Add("Mobile", GetType(String))
.Columns.Add("Aadhar", GetType(String))
End With
With DataGridView1
.Font = New Font(.Font.FontFamily, 16)
.DataSource = dt
.AllowUserToAddRows = False
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
With .DefaultCellStyle
.BackColor = Color.Blue
.ForeColor = Color.Yellow
End With
End With
End Sub
Sub GetData()
Dim ofd As New OpenFileDialog
With ofd
.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
.Filter = "Text Files|*.txt|All Files|*.*"
.FilterIndex = 0
End With
If ofd.ShowDialog = DialogResult.OK Then
dt.Clear()
Dim nr As DataRow = dt.NewRow
For Each line As String In IO.File.ReadAllLines(ofd.FileName)
If line.StartsWith("Village") Or line.StartsWith("Grower") Or line.StartsWith("Mobile") Or line.StartsWith("Aadhar") Then
Dim l2 As String = line.Replace(":", String.Empty)
Dim ind As Integer = l2.IndexOf(StrDup(16, " "), 1)
l2 = l2.Substring(0, ind)
Dim a() As String = l2.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Select Case a(0)
Case "Village"
nr(0) = a(1)
Case "Grower"
nr(1) = a(1)
Case "Mobile"
nr(2) = a(1)
Case "Aadhar"
nr(3) = a(1)
dt.Rows.Add(nr)
dt.AcceptChanges()
nr = dt.NewRow
End Select
End If
Next
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GetData()
End Sub
End Class