【发布时间】:2014-12-25 05:13:02
【问题描述】:
参考这个问题:StackOverflowException was unhandled in VB.NET 我决定创建一个新问题,因为我有一个新错误。
无法将“System.Windows.Forms.BindingSource”类型的对象转换为“System.Data.DataTable”类型。
代码(在按钮点击事件中):
' Copy rows from the first datagridview to the second datagridview that is data bound
' First copy the second datagridviews datasource into a new data table
Dim dt As DataTable = CType(frmEncodeDatabase.EncodingDataGridView.DataSource, DataTable).Copy
Dim dr As DataRow
' Loop through all rows of the first datagridview and copy them into the data table
For r As Int32 = 0 To Me.DataGridViewX1.Rows.Count - 1
If Me.DataGridViewX1.Rows(r).IsNewRow = False Then ' Do not copy the new row
dr = dt.NewRow
' Loop through all cells of the first datagridview and populate the data row
For c As Int32 = 0 To Me.DataGridViewX1.ColumnCount - 1
dr(c) = Me.DataGridViewX1.Rows(r).Cells(c).Value
Next
dt.Rows.Add(dr) ' Add the data row to the data table
End If
Next
Me.DataGridView2.DataSource = dt ' Rebind the second datagridview with the new table which has all the rows from both datagridviews
frmEncodeDatabase.show()
图像中的错误在dt As DataTable = CType(frmEncodeDatabase.EncodingDataGridView.DataSource, DataTable).Copy 中,错误现在就在其中。我将如何修改代码?
【问题讨论】:
标签: vb.net