问题:将多个行和列从一个datagridview(非数据绑定)移动到另一个datagridview,然后从另一个数据绑定的表单中显示第二个datagridview行。
开发平台:VB.NET 2008
问题描述:
anotherForm.Show()
?问题代码:
Dim dr = DirectCast(DataGridViewX1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row
frmEncodeDatabase.EncodingCompleteDataSet.Tables("EncodingComplete").ImportRow(dr)
EncodingCompleteDataSetTableAdapters.EncodingCompleteTableAdapter.Update(EncodingCompleteDataSet, "EncodingComplete")
此致 CodingSource
编辑:对于Jason Faulkner
代码:
For Each Col As DataGridViewColumn In DataGridViewX1.Columns
frmEncodeDatabase.EncodingCompleteDataGridView.Columns.Add(DirectCast(Col.Clone, DataGridViewColumn))
Next
frmEncodeDatabase.EncodingCompleteDataGridView.Rows.Add(DataGridViewX1.Rows(0).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
你的代码(我改了一点):
Dim table As DataTable = DirectCast(frmEncodeDatabase.EncodingCompleteDataGridView.DataSource, DataView).Table
' Add row 0 to the DataGridViewX1 grid to the table.
' This assumes the schema/column names are the same between the two.
Dim addRow As DataRow = table.NewRow
For Each Col As DataGridViewColumn In DataGridViewX1.Columns
addRow(Col.Clone) = frmEncodeDatabase.EncodingCompleteDataGridView.Rows(0).Cells(Col.Clone).Value
Next
' Add the copied row values to the table.
' Once we do this, it should appear in the EncodingCompleteDataGridView.
table.Rows.Add(addRow)
但我在table As DataTable = DirectCast(frmEncodeDatabase.EncodingCompleteDataGridView.DataSource, DataView).Table
中出现错误,说
InvalidCastException未处理:无法转换类型的对象 ' System.Windows.Forms.BindingSource'输入' System.Data.DataView'。
答案 0 :(得分:0)
'This is your new DataGridView
Dim grdNew As New DataGridView
'This adds the row to your newDataGridView
For Each item In Me.DataGridView1.Rows
grdNew.Rows.Add(item)
Next