如何使用VB将逗号分隔的文本文件数据显示到数据网格视图中? [英] How to display comma separated text file data into a data grid view using VB?

查看:73
本文介绍了如何使用VB将逗号分隔的文本文件数据显示到数据网格视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将库存信息保存到文本文件中,现在我需要将保存到文本文件中的数据显示到数据网格视图中,我尝试使用下面的代码,但它部分工作,而不是分割数据它在一个单元格中加载了一整行数据(来自文本文件),我应该能够拆分该行,然后将其显示在数据网格的一行中。有没有人有任何想法?谢谢。



我的尝试:



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")

推荐答案

使用此函数将其读入a DataTable:快速CSV阅读器 [ ^ ]

然后使用该表作为DataSource DataGridView的参数。
Use this to read it into a DataTable: A Fast CSV Reader[^]
Then use the table as the DataSource parameter of your DataGridView.


检查我过去的答案:

如何处理单元格值2.72E + 11 [ ^ ]

如何读取文本文件数据并使用VB.NET在datagridview中显示 [ ^ ]
Check my past answers:
How to handle cell value "2.72E+11"[^]
How to read text file data and display in datagridview with VB.NET[^]


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();
}


这篇关于如何使用VB将逗号分隔的文本文件数据显示到数据网格视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆