Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

For Each Col As DataGridViewColumn In DataGridViewX1.Columns
        frmEncodeDatabase.EncodingCompleteDataGridView.Columns.Add(DirectCast(Col.Clone, DataGridViewColumn))
    frmEncodeDatabase.EncodingCompleteDataGridView.Rows.Add(DataGridViewX1.Rows(0).Cells.Cast(Of DataGridViewCell).Select(Function(c) c.Value).ToArray)

Dim dr = DirectCast(DataGridViewX1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row
    EncodingCompleteDataSetTableAdapters.EncodingCompleteTableAdapter.Update(EncodingCompleteDataSet, "EncodingComplete")

In the button1 the error is shown at this image:[^]

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?
Updated 23-Dec-14 2:41am
Sergey Alexandrovich Kryukov 23-Dec-14 10:17am    
Why? Why not developing proper code design: data layer separated from UI, populate UI from data layer, etc..?
Kschuler 23-Dec-14 10:34am    
Try adding the row to the DataSet that the grid is bound to instead of the grid itself.

1 solution

Howdy All,
I would like to show this example of moving data between two DataGridViews which are contained within separate forms. This demonstrates the idea Kschuler pointed out "adding row to dataset" vs. dealing with the datagridview members themselves. Also this demonstrates (briefly) separation of UI and data operations. It is contained in a single Module, it has an App class that moves the data around, a form class, an eventargs class to raise a event and a dataRec class that represents the datagridview datasource type (125 lines of code, so it isn't to hard to digest).

Regards ron O.

Imports System.Windows.Forms
Module Module1
    Sub main()
        Dim app As New app
    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()
        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
            End If
        End Sub
        'This updates the datagridview
        Private Sub RefreshGrids()
            Dim cm1 As CurrencyManager = DirectCast(_frm1.BindingContext(_frm1.dg.DataSource), CurrencyManager)
            Dim cm2 As CurrencyManager = DirectCast(_frm2.BindingContext(_frm2.dg.DataSource), CurrencyManager)
        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
                    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
                dg.DataSource = _list
            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.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)
            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
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
        Private _description As String
        Public Property Description() As String
                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
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900