Click here to Skip to main content
15,666,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Great Minds !
I have Difficulties to read text where notepad have large data and strings character like ---- and ...
The Data i Want to read data in datagrid Like Below

Village Grower Mobile Aadhar
50305 301 12323555555 555444566646446
50306 303 22335566644 565456467653436
continued..
But my problem is i am unable to load datagrid bcz its read all line of all text.
The Text File Like below
                                        Report For Print
District :XYZ                       Season :21 Run Date :18/03/2023       Page No :1
------------------------------------------------------------------------------------------------------------------
Cane Society :ABC                        Factory Name : SSD                           ANC
Village :     50305 Dummy Name                                                        Year 17-18 :    73.51  Qtl
Grower  :       221 Dummy Name                                                        Year 18-19 :    72.47  Qtl
UniCode :52013623210166                     Distt Avg Yield : 856.44 Qtl/Hect         Year 19-20 :    74.95  Qtl
MemberShip Year :2017                       Fact Avg. Sup.: 469                    Year 20-21 :    62.67  Qtl
Father  :           DummyName               A/C No : XXXXXXXXXXXXXXXX4340      Year 21-22 :    68.61  Qtl
Supply Mode :CART-18(18 Qtls.)              Bank : Dummy Bank                  
Ind Cnt :       504 Dummy                     Branch : ??? ?? ??????? ?????                    
Sup Cnt :       504 Dummy                    Old CLA :         0.091 Hect        Avg 2 Year :   65.64 Qtl
Mobile  :    1234567890                         Cane Area :         0.169 Hect        Avg 3 Year :   68.74 Qtl
Aadhar  :  12345667789899                       Basic Quota :         70.00 Qtl         Avg 5 Year :   70.44 Qtl
Gata wise Corrected CLA:0.096
==================================================================================================================
                         ER     EP(A)       EP       GR    GP(A)       GP       RR       RP     Total
==================================================================================================================
Area (Hect)       :    0.013    0.000    0.156    0.000    0.000    0.000    0.000    0.000     0.169
Effct Area (Hect) :    0.007    0.000    0.084    0.000    0.000    0.000    0.000    0.000     0.091
Production (Qtls) :     6.00     0.00    71.94     0.00     0.00     0.00     0.00     0.00     77.94
85 % Prod (Qtls)  :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Bond (Qtls)       :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Purchies in Basic :        1        0        6        0        0        0        0        0         7
Mode(9 Qtls)                      Total Purchee(As per Supply Mode) In CART-18(18 Qtls.)  =   4
==================================================================================================================


                                        Report For Print
District :XYZ                       Season :21 Run Date :18/03/2023       Page No :1
------------------------------------------------------------------------------------------------------------------
Cane Society :ABC                        Factory Name : SSD                           ANC
Village :     50306 Dummy Name                                                        Year 17-18 :    73.51  Qtl
Grower  :       226 Dummy Name                                                        Year 18-19 :    72.47  Qtl
UniCode :52013623210166                     Distt Avg Yield : 856.44 Qtl/Hect         Year 19-20 :    74.95  Qtl
MemberShip Year :2017                       Fact Avg. Sup.: 469                    Year 20-21 :    62.67  Qtl
Father  :           DummyName                  A/C No : XXXXXXXXXXXXXXXX4340      Year 21-22 :    68.61  Qtl
Supply Mode :CART-18(18 Qtls.)                   Bank : Dummy Bank                  
Ind Cnt :       504 Dummy                     Branch : ??? ?? ??????? ?????                    
Sup Cnt :       504 Dummy                    Old CLA :         0.091 Hect        Avg 2 Year :   65.64 Qtl
Mobile  :    2233445589                        Cane Area :         0.169 Hect        Avg 3 Year :   68.74 Qtl
Aadhar  :  45678888888888                       Basic Quota :         70.00 Qtl         Avg 5 Year :   70.44 Qtl
Gata wise Corrected CLA:0.096
==================================================================================================================
                         ER     EP(A)       EP       GR    GP(A)       GP       RR       RP     Total
==================================================================================================================
Area (Hect)       :    0.013    0.000    0.156    0.000    0.000    0.000    0.000    0.000     0.169
Effct Area (Hect) :    0.007    0.000    0.084    0.000    0.000    0.000    0.000    0.000     0.091
Production (Qtls) :     6.00     0.00    71.94     0.00     0.00     0.00     0.00     0.00     77.94
85 % Prod (Qtls)  :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Bond (Qtls)       :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Purchies in Basic :        1        0        6        0        0        0        0        0         7
Mode(9 Qtls)                      Total Purchee(As per Supply Mode) In CART-18(18 Qtls.)  =   4
==================================================================================================================


What I have tried:

VB
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
<code>    <pre lang="VB">
End Sub
Posted
Updated 19-Mar-23 1:03am
Comments
Dave Kreskowiak 19-Mar-23 12:04pm    
It's not a "Notepad file". It's a text file.

1 solution

The code you show doesn't try to do what you describe: in fact it doesn't do anything useful at all.
It looks like you found some "read a file" code and threw two lines into it in the hope it would work:
C#
ResultText.Text = line.ToString
'Datagridview1.DataSource = line
And it's pretty clear that those two lines weren't written by the author of the other lines - they show a complete lack of understanding of strings, DataGridViews, DataSources, or the file format. Heck, the samples you show as "I want to see this" don't even match the data in your file sample!

Go back to the beginning, and look at the file: it contains "records" which span multiple lines - so reading individual lines and trying to put them to a DGV isn;t goign to work.

Write a method which splits the file into actual records:
                                        Report For Print
District :XYZ                       Season :21 Run Date :18/03/2023       Page No :1
------------------------------------------------------------------------------------------------------------------
Cane Society :ABC                        Factory Name : SSD                           ANC
Village :     50305 Dummy Name                                                        Year 17-18 :    73.51  Qtl
Grower  :       221 Dummy Name                                                        Year 18-19 :    72.47  Qtl
UniCode :52013623210166                     Distt Avg Yield : 856.44 Qtl/Hect         Year 19-20 :    74.95  Qtl
MemberShip Year :2017                       Fact Avg. Sup.: 469                    Year 20-21 :    62.67  Qtl
Father  :           DummyName               A/C No : XXXXXXXXXXXXXXXX4340      Year 21-22 :    68.61  Qtl
Supply Mode :CART-18(18 Qtls.)              Bank : Dummy Bank                  
Ind Cnt :       504 Dummy                     Branch : ??? ?? ??????? ?????                    
Sup Cnt :       504 Dummy                    Old CLA :         0.091 Hect        Avg 2 Year :   65.64 Qtl
Mobile  :    1234567890                         Cane Area :         0.169 Hect        Avg 3 Year :   68.74 Qtl
Aadhar  :  12345667789899                       Basic Quota :         70.00 Qtl         Avg 5 Year :   70.44 Qtl
Gata wise Corrected CLA:0.096
==================================================================================================================
                         ER     EP(A)       EP       GR    GP(A)       GP       RR       RP     Total
==================================================================================================================
Area (Hect)       :    0.013    0.000    0.156    0.000    0.000    0.000    0.000    0.000     0.169
Effct Area (Hect) :    0.007    0.000    0.084    0.000    0.000    0.000    0.000    0.000     0.091
Production (Qtls) :     6.00     0.00    71.94     0.00     0.00     0.00     0.00     0.00     77.94
85 % Prod (Qtls)  :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Bond (Qtls)       :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Purchies in Basic :        1        0        6        0        0        0        0        0         7
Mode(9 Qtls)                      Total Purchee(As per Supply Mode) In CART-18(18 Qtls.)  =   4
==================================================================================================================
Is one record:
                                        Report For Print
District :XYZ                       Season :21 Run Date :18/03/2023       Page No :1
------------------------------------------------------------------------------------------------------------------
Cane Society :ABC                        Factory Name : SSD                           ANC
Village :     50306 Dummy Name                                                        Year 17-18 :    73.51  Qtl
Grower  :       226 Dummy Name                                                        Year 18-19 :    72.47  Qtl
UniCode :52013623210166                     Distt Avg Yield : 856.44 Qtl/Hect         Year 19-20 :    74.95  Qtl
MemberShip Year :2017                       Fact Avg. Sup.: 469                    Year 20-21 :    62.67  Qtl
Father  :           DummyName                  A/C No : XXXXXXXXXXXXXXXX4340      Year 21-22 :    68.61  Qtl
Supply Mode :CART-18(18 Qtls.)                   Bank : Dummy Bank                  
Ind Cnt :       504 Dummy                     Branch : ??? ?? ??????? ?????                    
Sup Cnt :       504 Dummy                    Old CLA :         0.091 Hect        Avg 2 Year :   65.64 Qtl
Mobile  :    2233445589                        Cane Area :         0.169 Hect        Avg 3 Year :   68.74 Qtl
Aadhar  :  45678888888888                       Basic Quota :         70.00 Qtl         Avg 5 Year :   70.44 Qtl
Gata wise Corrected CLA:0.096
==================================================================================================================
                         ER     EP(A)       EP       GR    GP(A)       GP       RR       RP     Total
==================================================================================================================
Area (Hect)       :    0.013    0.000    0.156    0.000    0.000    0.000    0.000    0.000     0.169
Effct Area (Hect) :    0.007    0.000    0.084    0.000    0.000    0.000    0.000    0.000     0.091
Production (Qtls) :     6.00     0.00    71.94     0.00     0.00     0.00     0.00     0.00     77.94
85 % Prod (Qtls)  :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Bond (Qtls)       :     5.10     0.00    61.15     0.00     0.00     0.00     0.00     0.00     66.25
Purchies in Basic :        1        0        6        0        0        0        0        0         7
Mode(9 Qtls)                      Total Purchee(As per Supply Mode) In CART-18(18 Qtls.)  =   4
==================================================================================================================
Is another.
Then write a method that processes a single record and returns a class containing the relevant information as separate Public properties.

Then add each of those class instances to a collection - a List for example - and set that collection as the DGV DataSource.

Give it a try - this isn't complicated but it is your task: and we aren't here to do it all for you!
 
Share this answer
 
Comments
Amitkumaryadava 19-Mar-23 7:58am    
Thank you for your reply. If u provide me a code then i will be very thankful to you and let me clear sir i am beginner not a programmer and doing this task for my own print
OriginalGriff 19-Mar-23 8:11am    
And if I "give you a code" you will learn nothing, just as you have from your other questions. You've been here for two and a half years, and asking questions for four months - none of which have been "beginner questions".

So either you've blagged your way into a job you can't do, or you just can't be bothered to learn how to code on your own.
Either way, giving you "a code" doesn't help you in the long run - the only way you develop a skill is to use it yourself, not watch what others have done ...

This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Dave Kreskowiak 19-Mar-23 12:06pm    
What makes you think anyone is going to do your work for you?

That's insulting to everyone you ask, everyone you work with, and especially YOURSELF. You give yourself no credit or ability to learn how to do this yourself.

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