Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have saved stock information into a text file now I would need to display the data I have saved into the text file into a data grid view, I have tried using the code below but it's working partially, instead of splitting the data it's loading a whole row of data (from the text file) in one cell, I should be able to split this row and then display it in one row of the data grid. Has anyone got any idea?Thank you.

What I have tried:

Dim STUDENT_FILE As String = "Z:\Desktop\Customers.txt"
       Dim objReader As New System.IO.StreamReader(STUDENT_FILE)

       Dim strDataline As String 'Data line
       Dim strArr(2) As String ' Array for bits of line
       Dim blfound As Boolean
       ListBox1.Items.Clear()


       Do While objReader.Peek() <> -1 'read the file till the end.
           strDataline = (objReader.ReadLine()) ' read line and store into variable
           strArr = strDataline.Split(",") 'Split line and put into array

           DataGridView1.Rows.Add(strDataline)
           blfound = True

       Loop
       MsgBox("stock data has been loaded")
       objReader.Close()
       If blfound = False Then MsgBox("stock data has not been found")
Posted
Updated 12-Feb-18 8:19am

Use this to read it into a DataTable: A Fast CSV Reader[^]
Then use the table as the DataSource parameter of your DataGridView.
 
Share this answer
 
protected void ImportCSV(object sender, EventArgs e)
{
    //Upload and save the file
    string csvPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(csvPath);
 
    //Create a DataTable.
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
        new DataColumn("Name", typeof(string)),
        new DataColumn("Country",typeof(string)) });
 
    //Read the contents of CSV file.
    string csvData = File.ReadAllText(csvPath);
 
    //Execute a loop over the rows.
   foreach (string row in csvData.Split('\n'))
    {
        if (!string.IsNullOrEmpty(row))
        {
            dt.Rows.Add();
            int i = 0;
 
            //Execute a loop over the columns.
            foreach (string cell in row.Split(','))
            {
                dt.Rows[dt.Rows.Count - 1][i] = cell;
                i++;
            }
        }
    }
 
    //Bind the DataTable.
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
 
Share this answer
 
 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!!
 
Share this answer
 
Comments
[no name] 16-Feb-18 9:48am    
You don't ask questions within answers, you've already been told to post a separate thread.

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