简体   繁体   English

在VB.NET中向DataGridView添加行

[英]Adding rows to DataGridView in VB.NET

I have a DataGridView set up with the following columns: 我有一个DataGridView设置与以下列:

Teacher, Subject, Date, Period. 老师,主题,日期,期间。

After a lot of Googleing I can see there are a few ways to add data to the grid programmatically, with each one differing to another quite extensively. 经过大量的Googleing之后,我可以看到有几种方法可以通过编程方式将数据添加到网格中,每种方法都相当不同。

I wanted your opinion on how I should go about this, considering I am going to be adding data from a text file line-by-line (using ":" as a delimiter) and I want each line to have its own row, so it's going to be in a loop. 考虑到我将逐行添加来自文本文件的数据(使用“:”作为分隔符),我希望你对我应该如何做到这一点,我希望每行都有自己的行,所以它会在一个循环中。

Thanks. 谢谢。

Since the data in the text file is delimited then one way to do this is to use the Split function to create an array of cell strings and then just add these directly to the grid. 由于文本文件中的数据是分隔的,因此一种方法是使用Split函数创建单元字符串数组,然后直接将它们添加到网格中。

Dim CellData() As String
Dim LineText As String = ""

' open the data file
Dim objReader As New System.IO.StreamReader("c:\temp\file.dat")

Do While objReader.Peek() <> -1
    LineText = objReader.ReadLine()
    ' split the line of text into cells
    CellData = Split(LineText, ":")
    Me.DataGridView1.Rows.Add(CellData)
Loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM