0

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

当我双击FullSelectedRowDataGridView1SelectedRow应该将 复制到DataGridView2,两个网格都DataBounded来自 SQL Server 2008..

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

**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,它给了我一个错误Object reference not set to an instance of an object.

4

1 回答 1

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
于 2013-01-21T14:15:28.033 回答