如何在DataGridView中处理KeyDown事件时解决InvalidOperationException?

时间:2014-05-20 14:05:12

标签: vb.net datagridview invalidoperationexception

我正在尝试为我的DataGridView“复制和粘贴”。当我尝试运行我的代码时,我收到了InvalidOperationException和其他信息: “当控件受数据限制时,无法以编程方式将行添加到DataGridView的行集合中。”

我试图在线搜索,但是我收到的信息太多了,大多数都不相关。你们对如何解决这个问题有什么想法吗?

这些是处理我的KeyDown事件的代码:

If e.Control And (e.KeyCode = Keys.V) Then
            Dim selectedRowIndex As Integer = Me.DataGridView2.SelectedCells(0).RowIndex
            Dim i As Integer = Me.DataGridView2.Rows.AddCopy(selectedRowIndex) '<<It appears that this line has the problem

            'copy the values of the row positioned at selectedRowIndex to the new row
            Me.DataGridView2.Rows(i).Cells(0).Value = Me.DataGridView2.Rows(selectedRowIndex).Cells(0).Value
            Me.DataGridView2.Rows(i).Cells(0).Value = Me.DataGridView2.Rows(selectedRowIndex).Cells(1).Value
            Me.DataGridView2.Rows(i).Cells(0).Value = Me.DataGridView2.Rows(selectedRowIndex).Cells(2).Value
            Me.DataGridView2.Rows(i).Cells(0).Value = Me.DataGridView2.Rows(selectedRowIndex).Cells(3).Value
            Me.DataGridView2.Rows(i).Cells(0).Value = Me.DataGridView2.Rows(selectedRowIndex).Cells(4).Value

        End If

提前多多感谢!

0 个答案:

没有答案