hi all..

i have the option to save the contents of the listview that was saved into text file.
now i would like to load the text file into datagridview in vb.net but i have no idea how to do it as this is my first time in doing it and i had googled around but couldn't find the information i needed.

can anybody show me example of code to load text file data into datagridview? i am using vb.net and mysql

thanks in advance!!

Can you please clarify? What are the contents of the text file and how do you want it parsed to go into the datagrid? What is the layout of the datagrid? An example might be "I have a text file containing lines of comma separated values. I want to display them in a datagrid with one value per cell and one line per row."

sorry, u can ignore about mysql..
the contents are:
(name) (date) (charges)
LEE,06/08/2011,1000.00
which the contents are seperated by commas
i want it to show one line per row. sorry for my bad english and explanation

I created a form with a datagridview control named grdMyData. It has three columns - colName, colDate and colCharges. To add one row of new data I did the following:

Dim dgrow As New DataGridViewRow
Dim dgcell As DataGridViewCell

dgcell = New DataGridViewTextBoxCell
dgcell.Value = "John"
dgrow.Cells.Add(dgcell)

dgcell = New DataGridViewTextBoxCell
dgcell.Value = "2011-10-30"
dgrow.Cells.Add(dgcell)

dgcell = New DataGridViewTextBoxCell
dgcell.Value = "$12.99"
dgrow.Cells.Add(dgcell)

grdMydata.Rows.Add(dgrow)

Ii hope this helps.

PS, my code has "Imports System.Windows.Forms" at the top.

but if there are many rows per line, how should i read it into datagridview?

Can you give me an actual sample of the input file so I know what you mean?

the listview have 3 columns, and have few rows containing some data..
i have save the content into textfile and the output is like

LEE,06/08/2011,1000.00
KEVIN,08/08/2011,1600.00
SALAU,15/08/2011,800.00
..etc..
..etc..

the textfile data then i intend to insert into datagridview after opening the file..

However,i am now saving the listview item into csv file and then able to insert into datagridview.
But, i would be more than happy if you don't mind to show me on how to insert the textfile into DGV..
thank you very much for your reply too!! =D

Try this

'read all lines from the file into a string array (one line per string)

Dim lines() As String = My.Computer.FileSystem.ReadAllText("d:\temp\sample.txt").Replace(vbLf, "").Split(vbCr)

Dim dgrow As DataGridViewRow
Dim dgcell As DataGridViewCell

'insert each line of input into a row in the datagrid

For Each line As String In lines

    Dim fields() As String = line.Split(",")

    'ignore lines that don't have three fields

    If UBound(fields) = 2 Then

	dgrow = New DataGridViewRow

	For i As Integer = 0 To 2
	    dgcell = New DataGridViewTextBoxCell
	    dgcell.Value = fields(i)
	    dgrow.Cells.Add(dgcell)
	Next

	grdMydata.Rows.Add(dgrow)

    End If

Next

But I still don't know what you mean by many rows per line. You data file has one row per line and I assume you want to display it that way in the datagrid.

Hey, It seems I'm trying to do something similar to the OP here but am having a little trouble due to my novice skills. Basically I have some data "Name, Type, Stocklvl, Price, Location, Reorderlvl" which I need to load and display, preferably with options for editing. Then once the stocklvl drops below the reorderlvl I need to have details on the item and stock saved to another Reorder text file.

So I know I'll need to change the integer part to suit. But don't know how to get this data back into another text file in the same format from my data grid as well as updating it in my main text file rather than just loading from it. So far trying to implement the above code I'm getting the error 'Argument not specified for parameter 'FileNumber' of 'Public Function LineInput(FileNumber As Integer) As String' as I replaced line.split (on line 12 of above) with Microsoft.VisualBasic.FileSystem.LineInput.Split as VB 2010 suggested.

Any help would greatly be appreciated and sorry for reviving an old thread, I just thought it more appropriate than creating a new one.

Please don't hijack dead threads.

Please start a thread of your own.

Thank you.

I'm sorry. I'm just so use to being told off for creating new threads when it's not neccesary and I did have some questions about the code here...

We encourage people to start their own threads, just as long as they close them when their question has been answered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.