datagridview to/from a textfile

alfizo

New member
Joined
Aug 19, 2008
Messages
2
Location
Pretoria
Programming Experience
Beginner
Hey guys i was wondering if you could help me with A Code to load information that was saved to a textfile to load back into the datagrid... please help me!

i tried the following code but it dont load it back!

VB.NET:
' Create an instance of StreamReader to read from a file.
        Using sr As IO.StreamReader = New IO.StreamReader("e:\moviedatasource.txt")
            Dim line As String
            ' Read and display the lines from the file until the end 
            ' of the file is reached.
            Do

                For Each currentrow As DataGridViewRow In Me.DataGridView.Rows

                Next

              
                line = sr.ReadLine()
                System.Diagnostics.Debug.WriteLine(line)
            Loop Until line Is Nothing
            sr.Close()
        End Using


please help guys
 
Last edited:
Saving Data from a datagridview to a textfile

Hey guys i was wondering if i could be assisted with the following! the following code i used is to save information from a datagrid to a textfile but the problem is that every time i SAVE it discards the information previously stored as it starts saving from the beginning of file. i want someone to help me with the code to append the data an not over write the previously stored info....




VB.NET:
Using theWriter As New System.IO.StreamWriter("c:\moviedatasource.txt")
    For Each currentrow As DataGridViewRow In Me.DataGridView.Rows
        ' The Cells are the Columns
        For Each currentcolumn As DataGridViewCell In currentrow.Cells
            'objfile.WriteLine(currentcolumn.Value + ",")
            theWriter.Write(currentcolumn.Value & " ")
        Next
        theWriter.WriteLine()
    Next
    theWriter.Close()
End Using 'display message information saved
MessageBox.Show("Information Saved", "Saved!")
 
With the StreamWriter class constructor you can specify Append parameter if you want to append or not.

Since you have added the fields together with spaces and you need to split up that string again. Guess what, String class has a Split method that you can use. Another option is inserting the "Read a delimited text file" snippet, see this post.
 
Help needed

Hey alfizo..wsup?
did u figure out a way of reading from the text file and display it in the datagrid(and vice versa)?? Please help me coz i am also stuck wid da same problem..
Your help will be highly appreciated.
Thanx
 
Wow talk about digging up a post from a half year ago...

Jisty give me more details about your program, the file your trying to read and where exactly your stuck (hate just giving the whole answer without making ya work for it...)
 
Wsup Tom!
Thanx for replying..I am using visual basic 2005 n i successfully managed to save the data from the datagrid to the text file using the streamwriter method..But now i used the streamreader method as shown in this post above to read the data from the text file to the datagrid but its not showing anything..Dunno wat da problem is
And Is there another easier method to read/write from the text file to a datagrid..
Thanx for helping man!!
 
Again I would still need the details of your file to be read... Is it a tab delimited, comma delimited, fixed length? The original poster has split his columns by a single space, does your file structure match this in that every space is the delimiter to identifying a new column/field in your file? What does your table structure look like and will the data interact with a database?

Its sounds like you havent figured out how you want to save the info yet. I prefer XML over delimited files, no need to read & write each line of data in the file but since I dont know what your data contains or how it will be used, I would need more details...
 
Hi Tom,
Ok i used da exact same code of streamwriter as shown above in this thread..In the text file the data is seperated by one space..hence the next value goes into another column.
My datagrid has two columns: Details & Amount..It is a very simple datagrid which is used to enter the details of the item purchased and its price for every single customer. It is not connected to any database.I jus want that data in the datagrid to be saved..and also to be retrieved when browsing through a specific customer's page..That is, each and every customer has its own values in the datagrid.

How can i use XML in visual basic 2005?

Thanx for ur help!
 
Working with Xml is real easy and very flexible to add things like more columns of data, more table etc in the future if your requirements change etc. You will have to familarize yourself with datasets/datatables and navigating the records for them.

Attached is a simple example and I think most of which is self explainatory. Feel free to ask any questions you do have about it though. To start I created a Typed Dataset with a single table (tblInvoice) which contains your two specified columns. Reading & writing the results to an xml file is as simple as calling Dataset.ReadXml(strFileName) & DataSet.WriteXml(strFileName).

The table records are bound and displayed in a DataGridView control. To add records to the table you can type them directly into the last row of the DGV. Also I added a button that programitically created 100 dummy records for the table.
 

Attachments

  • Invoice.zip
    19.4 KB · Views: 115
I tried opening the project but it displays an error while opening.. I am attaching a print screen of the error.
 

Attachments

  • untitled.zip
    45.5 KB · Views: 51

Latest posts

Back
Top