我继续将旧的VB6重写为.Net作为我的.Net学习的一部分。我已经设法将CSV文件读入Datatable,当我查看'dt'时,我可以使用'datatable visualiser'查看它已正确填充,但它似乎没有填充我试图绑定的DataGridView控件它来。我花了几个小时浏览其他线程和尝试的东西,但我看不出我做错了什么。 DataGridView在App中的另一个表单上,我正在从主MDI表单中读取CSV文件(各种配置文件)。当我显示包含DataGridView的子窗体时,它总是为空(它实际上似乎添加了两个数据表行,但它们的内容都是空白的。)
Public dt As New DataTable
Public ThisFilename As String = "c:\SitesDB.cfg"
Private Sub GetSitesDB()
Dim sr As New IO.StreamReader(ThisFilename)
Dim newline() As String = sr.ReadLine.Split(","c)
dt.Columns.AddRange({New DataColumn(newline(0)), New DataColumn(newline(1)), New DataColumn(newline(2)), New DataColumn(newline(3)),
New DataColumn(newline(4)), New DataColumn(newline(5)), New DataColumn(newline(6)), New DataColumn(newline(7)), New DataColumn(newline(8)),
New DataColumn(newline(9)), New DataColumn(newline(10)), New DataColumn(newline(11))})
While (Not sr.EndOfStream)
newline = sr.ReadLine.Split(","c)
Dim newrow As DataRow = dt.NewRow
newrow.ItemArray = {newline(0), newline(1), newline(2), newline(3), newline(4), newline(5), newline(6), newline(7), newline(8), newline(9), newline(10), newline(11)}
dt.Rows.Add(newrow)
End While
Form1.DataGridView1.DataSource = dt ' data can be seen here using datatable visualiser hovering over 'dt'
End Sub
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
Form1.Show()
End Sub
我尝试刷新DataGridView,但无济于事。我想知道在将数据表绑定到DataGridView之后是否与Form1.Show()有关?
但以下没有帮助
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' chkBit.Checked = False
' Dim cfgtext As String : cfgtext = ""
DataGridView1.DataSource = NAMS.dt
End Sub