Click here to Skip to main content
15,891,864 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:

Button1:
VB
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:
VB
Dim dr = DirectCast(DataGridViewX1.Rows(e.RowIndex).DataBoundItem, System.Data.DataRowView).Row
    frmEncodeDatabase.EncodingCompleteDataSet.Tables("EncodingComplete").ImportRow(dr)
    EncodingCompleteDataSetTableAdapters.EncodingCompleteTableAdapter.Update(EncodingCompleteDataSet, "EncodingComplete")


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?
Posted
Updated 23-Dec-14 2:41am
v3
Comments
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..?
—SA
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
        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
 
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