我不确定是不是因为数据表没有正确填写(因此是空的,没有任何内容可显示),或者我是不是将它加载到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 = 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中没有列时,没有任何反应。
如何使数据表动态添加列数?