简体   繁体   English

试图将datagridview从一种形式复制到另一种形式

[英]Trying to copy a datagridview from one form to another

I know my title is a little strange. 我知道我的头衔有点奇怪。 Let me explain. 让我解释。 I am trying to "merge" two databases, one we house locally and one that is at another facility with the hopes of avoiding our admin group having to duplicate entry through separate forms. 我正在尝试“合并”两个数据库,一个在本地存放,另一个在另一家存放,希望避免我们的管理员组不得不通过单独的表格重复输入。 I have the source for a webservice that connects to the offsite database and I can enter data. 我有一个连接到非现场数据库的Web服务的源,可以输入数据。 What I am trying to do is expand that source by adding in our own database and copying our data to theirs. 我正在尝试通过添加我们自己的数据库并将数据复制到他们的数据库来扩展该源。 Hope this makes sense. 希望这是有道理的。

Anyway, 无论如何,

So both the local and offsite forms have datagridviews attached to them. 因此,本地和非现场表单都具有附加的datagridviews。 I can currently copy the contents of the first row to their form for entry, but I am getting errors when it attempts to go to the second row. 我目前可以将第一行的内容复制到其表单中以进行输入,但是当它尝试转到第二行时出现错误。 Here is my code: 这是我的代码:

  Dim i As Integer
        For i = 0 To VRMAdDataGridView.Rows.Count - 1
            'For Each row As DataGridViewRow In VRMAdDataGridView.Rows

            'myrow = VRMAdDataGridView.CurrentRow.Index
            frmcall.dgvRMAItems.Rows(i).Cells("cItemID").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn5").Value
            frmcall.dgvRMAItems.Rows(i).Cells("cExpectedSerialNumber").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn2").Value
            frmcall.dgvRMAItems.Rows(i).Cells("cNotes").Value = Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn3").Value

            'Format Type codes from FW to match Utica
            Select Case Me.VRMAdDataGridView.Rows(i).Cells("DataGridViewTextBoxColumn6").Value
                Case "Repair"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Repair"
                Case "RTS"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Rtn/Stock"
                Case "Repair !!RUSH!!"
                    frmcall.dgvRMAItems.Rows(i).Cells("cRequestedAction").Value = "Rush Repair"

            End Select


            ' myrowval = row.Cells("DataGridViewTextBoxColumn5").Value
            ' MsgBox(myrowval, MsgBoxStyle.Information, "Prod_Code")
            'Do Something
        Next

When I try to run the code I receive the following error: 当我尝试运行代码时,出现以下错误:

{"Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"} {“索引超出范围。必须为非负数并且小于集合的大小。参数名称:index”}

You should manipulate the underlying datasources, not UI elements (DataGridViews in this case). 您应该操作基础数据源,而不是UI元素(在这种情况下为DataGridViews)。 For example, your DataSource is a DataTable. 例如,您的数据源是一个数据表。 Then just use Clone method to get a copy. 然后,只需使用Clone方法即可获取副本。 Then set a DataSource of that other DataGridView. 然后设置该其他DataGridView的DataSource。 If you need something more than a copy, it's definitely going to be more simple to work with a series of DataRow objects. 如果您需要的不仅仅是副本,那么使用一系列DataRow对象肯定会更加简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM