Vb.net Gridview未从datatable填充
我不确定这是因为datatable填写不正确(因此为空,没有显示内容),还是因为我没有将其正确加载到gridview,但下面是我的代码:Vb.net Gridview未从datatable填充,vb.net,gridview,Vb.net,Gridview,我不确定这是因为datatable填写不正确(因此为空,没有显示内容),还是因为我没有将其正确加载到gridview,但下面是我的代码: Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'So Get all the Rows by splitting it on a NewLine Character Dim Rows() As String = Clip
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'So Get all the Rows by splitting it on a NewLine Character
Dim Rows() As String = Clipboard.GetText().Split(Environment.NewLine)
Dim Cols() As String
Dim DT As New DataTable
Dim DR As DataRow
For i As Int32 = 0 To Rows.Length - 1
'Then For Each Row get the Columns which are tab Seperated
Cols = Rows(i).Split(vbTab)
DR = DT.NewRow
'Now create a DataRow to be added to the underlying DataTable
For j As Int32 = 0 To Cols.Length - 1
'Now loop for each Column and Add the column data
If DT.Columns.Count > j Then
If (j = 2) Then
DR(j) = Convert.ToInt32(Cols(j))
Else
DR(j) = Cols(j)
End If
End If
Next
DT.Rows.Add(DR)
Next
DataGridView2.DataSource = DT
End Sub
编辑:当我在运行程序之前向gridview预添加列时,它将填充其中的空白数据。当我在gridview中没有预先添加的列时,什么也不会发生
如何使列数由datatable动态添加?这是
DT.columns.Count
大于0吗?为什么您认为应该始终大于0。使用F11运行代码以逐步完成,并在代码运行时查看变量值。我看不到你在哪里添加列。我看不到你在哪里将列添加到数据表中。谢谢,我会检查一下。这可能就是问题所在。