问题描述
亲爱的程序员,
我有2个表单,我想从Form1的datagridview转移一行到Form2的数据绑定数据网格。
我做了2个按钮用于试错,这是按钮的代码:
Button1:
Dear fellow programmers,
I have 2 forms and I want to transfer one row from Form1's datagridview to Form2's databound datagridview.
I made 2 buttons for trial and error purposes and this is the code of the buttons:
Button1:
For Each Col As DataGridViewColumn In DataGridViewX1.Columns
frmEncodeDatabase.EncodingCompleteDataGridView.Columns.Add(DirectCast(Col.Clone, DataGridViewColumn))
Next
frmEncodeDatabase.EncodingCompleteDataGridView.Rows.Add(DataGridViewX1.Rows(0).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)
Button2:
Button2:
Dim dr = DirectCast(DataGridViewX1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row
frmEncodeDatabase.EncodingCompleteDataSet.Tables("EncodingComplete").ImportRow(dr)
EncodingCompleteDataSetTableAdapters.EncodingCompleteTableAdapter.Update(EncodingCompleteDataSet, "EncodingComplete")
在按钮1中,此图像显示错误: []
在button2中,错误位于Update参数内的EncodingCompleteDataSet中,其中显示错误1EncodingCompleteDataS et'是一种类型,不能用作表达式。'
我该怎么办?我应该选择什么代码?
In the button1 the error is shown at this image: http://postimg.org/image/j3een1ewv/[^]
In the button2 the error is in the EncodingCompleteDataSet inside the Update argument, which says 'Error 1 'EncodingCompleteDataSet' is a type and cannot be used as an expression.'
What should I do then? What code should I choose?
推荐答案
Imports System.Windows.Forms
Module Module1
Sub main()
Dim app As New app
Application.Run()
End Sub
Public Class app
Dim _list1 As List(Of dataRec)
Dim _list2 As List(Of dataRec)
WithEvents _frm1 As MyForm
WithEvents _frm2 As MyForm
Public Sub New()
MadeLists()
AddForms()
End Sub
'This is where the rows are moved in/out
Private Sub changeRow(sender As Object, e As myEventArgs) Handles _frm1.ChangeRow, _frm2.ChangeRow
If _list1.Exists(Function(x) x.Name = e.Datarec.Name) Then
_list1.Remove(e.Datarec)
_list2.Add(e.Datarec)
Else
_list2.Remove(e.Datarec)
_list1.Add(e.Datarec)
End If
RefreshGrids()
End Sub
'This updates the datagridview
Private Sub RefreshGrids()
Dim cm1 As CurrencyManager = DirectCast(_frm1.BindingContext(_frm1.dg.DataSource), CurrencyManager)
cm1.Refresh()
Dim cm2 As CurrencyManager = DirectCast(_frm2.BindingContext(_frm2.dg.DataSource), CurrencyManager)
cm2.Refresh()
End Sub
Private Sub AddForms()
_frm1 = New MyForm("frm_1", _list1)
_frm2 = New MyForm("frm_2", _list2)
End Sub
Private Sub MadeLists()
_list1 = New List(Of dataRec)
_list1.Add(New dataRec("Ronald", "Programmer"))
_list1.Add(New dataRec("Jon", "Eukalali Player"))
_list1.Add(New dataRec("Dave", "Boss"))
_list1.Add(New dataRec("Hans", "Motor Cycle Rider"))
_list2 = New List(Of dataRec)
_list2.Add(New dataRec("Abbee", "Scienetist"))
_list2.Add(New dataRec("Gibbs", "Boss"))
_list2.Add(New dataRec("Di Nozo", "Cool Guy"))
_list2.Add(New dataRec("MC Gee", "One of US"))
End Sub
Friend Class myEventArgs
Inherits EventArgs
Private _datarec As dataRec
Public Property Datarec() As dataRec
Get
Return _datarec
End Get
Set(ByVal value As dataRec)
_datarec = value
End Set
End Property
Public Sub New(ByVal data As dataRec)
_datarec = data
End Sub
End Class
Friend Class MyForm
Inherits Form
Public Event ChangeRow(ByVal sender As Object, ByVal e As myEventArgs)
Friend WithEvents dg As System.Windows.Forms.DataGridView
Friend WithEvents Button1 As System.Windows.Forms.Button
Dim _list As List(Of dataRec)
Public Sub New(ByVal text As String, ByVal list As List(Of dataRec))
Me.Text = text
_list = list
init()
dg.DataSource = _list
Me.Show()
End Sub
Private Sub init()
Me.dg = New System.Windows.Forms.DataGridView()
Me.Button1 = New System.Windows.Forms.Button()
CType(Me.dg, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
Me.dg.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
Me.dg.Location = New System.Drawing.Point(2, 2)
Me.dg.Name = "dg"
Me.dg.Size = New System.Drawing.Size(322, 267)
Me.Button1.Location = New System.Drawing.Point(118, 291)
Me.Button1.Text = Me.Text
Me.ClientSize = New System.Drawing.Size(326, 326)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.dg)
Me.ResumeLayout(False)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If dg.CurrentRow IsNot Nothing AndAlso dg.CurrentRow.Index > -1 Then
RaiseEvent ChangeRow(Me, New myEventArgs(_list(dg.CurrentRow.Index)))
End If
End Sub
End Class
End Class
Public Class dataRec
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _description As String
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal desc As String)
_name = name
_description = desc
End Sub
End Class
End Module
这篇关于在VB.NET中将一个datagridview数据复制到另一个datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!