将行复制到另一个DataGridView

时间:2013-01-21 13:50:32

标签: vb.net datagridview datatable dataset rows

我在Windows窗体中有两个DataGridViews,VB.NET

当我双击FullSelectedRow的{​​{1}}时,DataGridView1应该被复制到SelectedRow,两个网格都是DataGridView2来自SQL Server 2008 ..

这是我在每个网格中检索数据的代码..

DataBounded

我在其他论坛上搜索,这就是我所拥有的......

**DataGridView1**
Dim UserScheduleString As String = "SELECT * FROM Schedule"
Dim UserScheduleStringList As New SqlCommand(UserScheduleString, mdl_Connection.CONN)

UserScheduleStringList.CommandType = CommandType.Text
UserScheduleStringListAdpt.SelectCommand = UserScheduleStringList
UserScheduleStringListAdpt.SelectCommand.ExecuteNonQuery()
UserScheduleStringListAdpt.Fill(UserScheduleStringListDataSet, "Users")
DataGridView1.DataSource = UserScheduleStringListDataSet.Tables("Users")


**DataGridView2**
Dim SomeTableString As String = "SELECT * FROM someTable"
Dim SomeTableStringList As New SqlCommand(SomeTableString, mdl_Connection.CONN)

SomeTableStringList.CommandType = CommandType.Text
SomeTableStringListAdpt.SelectCommand = SomeTableStringList
SomeTableStringListAdpt.SelectCommand.ExecuteNonQuery()
SomeTableStringListAdpt.FillSomeTableStringListDataSet, "*<tablename>*")
DataGridView1.DataSource = SomeTableStringListDataSet.Tables("*<tablename>*")

当我尝试将其放入newCustomersRow = UserScheduleStringListDataSet.Tables("Users").NewRow() newCustomersRow("ID") = "0023" newCustomersRow("Day") = "Tuesday" newCustomersRow("Start Time") = "13:00:00" newCustomersRow("End Time") = "18:00:00" UserScheduleStringListDataSet.Tables("Users").Rows.Add(newCustomersRow) UserScheduleStringListAdpt.Update(UserScheduleStringListDataSet, "Users") 时,它会给我一个错误DataGridView1_CellMouseDoubleClick

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

Private Sub DataGridView1_CellMouseDoubleClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) 

   Dim dr = DirectCast(DataGridView1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row

   UserScheduleStringListDataSet.Tables("Users").ImportRow(dr)

   UserScheduleStringListAdpt.Update(UserScheduleStringListDataSet, "Users")

End Sub