Hot to read Specific line of text in notepad and display in datagridview where value repeated multiple lines in a large notepad file

Amit kumar Yadava 81 Reputation points
2023-03-19T09:49:58.3166667+00:00

Datagridview

Hello Great Mind !

I am beginner in programming and still learning. This forum help me a lot to easy my job. I am not a programmer professionally, I do coding for my own works.

Previously I need help to write text in notepad and a user of this group help me a lot. I will be always thankful for him.

I have Another Problem related Notepad reading in vb.net.

I am Trying to read i notepad file and import data in data grid view. the text i need to read is in starting of each line. data grid should be as Like below

Village Code Grower Cd Mobile Aadhar

50305 221 1234567890 12345667789899

50306 226 2233445589 45678888888888 Local.txt

My Code Are as Below file is also attached with this request.

Please help me, I will be highly obliged to you

Dim FilePath As String

    Private Sub BtnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles BtnBrowse.Click
        Dim Ofd As New OpenFileDialog
        Ofd.Filter = "Notepad File|*.txt"
        Ofd.Title = "Select Text File "
        Ofd.Multiselect=False
        If Ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            If Not Ofd.FileName = String.Empty Then
                TxtFilePath.Text = Ofd.FileName
                FilePath = TxtFilePath.Text
            End If
        End If

    End Sub

    Private Sub BtnRead_Click(sender As System.Object, e As System.EventArgs) Handles BtnRead.Click
        Try

            Using sr As StreamReader = New System.IO.StreamReader(FilePath)
                Dim line = sr.ReadToEnd()

                ResultText.Text = line.ToString
                'Datagridview1.DataSource = line

            End Using
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,367 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,603 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2023-03-20T15:10:07.06+00:00

    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!

    111

    ' 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
    
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Amit kumar Yadava 81 Reputation points
    2023-03-19T16:06:10.9466667+00:00

    I Found a Solution But its repeating each lines contains count so file is bigger and values are multiple and duplicate. First my priority to write a text text in another file

    My code is

    Private Sub BtnRead_Click(sender As System.Object, e As System.EventArgs) Handles BtnRead.Click
            Try
                If File.Exists(IO.Path.Combine(Application.StartupPath, "Local.txt")) Then
                    File.Delete(IO.Path.Combine(Application.StartupPath, "Local.txt"))
                End If
                Dim Readtext As String() = File.ReadAllLines(FilePath)
                For linecount As Int32 = 0 To File.ReadLines(FilePath).Count()
                    For Each s In Readtext
                        If s.Contains("Village") Then
                            File.AppendAllText(IO.Path.Combine(Application.StartupPath, "Local.txt"), s.ToString + Environment.NewLine)
                        End If
                    Next
                Next
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    

  2. LesHay 7,126 Reputation points
    2023-03-20T12:02:35.97+00:00

    (DELETED - I hope! Maybe!

    0 comments No comments