Vb.net 使用新的datagrid视图将数据从datagrid视图复制到新表单
我正在使用VisualStudio2010。我已经创建了一个新的VB Windows窗体应用程序,它使用多个窗体 在其中一个表单(form2)上,我根据用户在form1上选择的许多项创建了datagridview 最终用户可以在datagridvew上添加信息,然后转到第三个表单(form3)。在第三个表单中,我想将与表单2中datagrid的前三列相同的信息复制到一个新的datagrid中 要从form1上的列表框中复制项目,我使用类似以下代码:Vb.net 使用新的datagrid视图将数据从datagrid视图复制到新表单,vb.net,winforms,forms,datagridview,Vb.net,Winforms,Forms,Datagridview,我正在使用VisualStudio2010。我已经创建了一个新的VB Windows窗体应用程序,它使用多个窗体 在其中一个表单(form2)上,我根据用户在form1上选择的许多项创建了datagridview 最终用户可以在datagridvew上添加信息,然后转到第三个表单(form3)。在第三个表单中,我想将与表单2中datagrid的前三列相同的信息复制到一个新的datagrid中 要从form1上的列表框中复制项目,我使用类似以下代码: For Each Feature As Obj
For Each Feature As Object in form1.listbox1.SelectedItems
Me.DatagridView(Col, Row).Value = DirectCast(CustomerFeature, DataRowView)("Feature").ToString()
Row +=1
Next
我想使用:
For Each row As DGVRow In form2.DatagridView.Rows
'add information to the new datagrid
Next
在我的表格3上。但是,当我尝试访问form2数据时,它不会在intellisense中显示,也不会让我手动键入信息。当我尝试运行程序时,我得到:
“错误1对非共享成员的引用需要对象引用。”
我甚至不能访问我在表单上声明的公共变量。我应该提到,form2被称为使用:
Private Sub BTN_Next_Click(sender As System.Object, e As System.EventArgs) Handles BTN_Next.Click
Dim frm_PI_01 As New form2(PIMethod, StartTime, TheUser, TheComputer)
frm_PI_01.ShowDialog()
End Sub
新方法使用:
Public Sub New(ByVal PIMethod As String, ByVal StartTime As Date, ByVal TheUser As String, ByVal TheComputer As String)
' This call is required by the designer.
InitializeComponent()
PI_Method = PIMethod
Start_Time = StartTime
The_User = TheUser
The_Computer = TheComputer
End Sub
数据网格在onload事件期间填充。“错误1对非共享成员的引用需要对象引用。”
如此消息所述:您需要使用New
sub创建form2
-类型为form2
的对象的实例,该实例是您在BTN\u Next\u单击
中创建的。但是局部变量frm_PI_01
只能在该函数内部使用
类名为form2
时,您只能使用Shared
成员或函数/sub
我的意见是,如果您需要将一些信息/变量从表单传输到另一个表单,那么在您的情况下(当您在创建表单时只传输一次数据时),请在表单构造函数中使用参数
将另一个参数添加到form3
构造函数中。例如,引用您的datagridview
(这可能不是将控件引用到另一个窗体的良好做法,最好使用所需的数据创建一些变量并引用该变量,但仅举个例子,这是最简单的方法):
之后,您可以在Load
功能中使用它
'Of course create private variable for datagridview
private _dgvForm2 as dataGridView
//Constructor of form3
Public Sub New(dgvForm2 as DataGridView)
' This call is required by the designer.
InitializeComponent()
Me._dgvForm2 = dgvForm2
End Sub