locked
Update MDI Child DatagridView from another MDI Child RRS feed

  • Question

  • Hi,

    I have 3 forms, Form1 = MDI parent, Form2 = Input Form (MDI Child), Form3 = Datagrid form (MDI Child)

    While trying to update data from form2 to form3 we get error but the same works if form2 and form3 are not a MDI Child.

    Please help.

    And below is the code:

    MDI Parent:

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            f.MdiParent = Me
            f.Show()
    
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim f As New Form3
            f.MdiParent = Me
            f.Show()
        End Sub
    End Class

    Child Form 1:

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            With Form3
                Dim row As String() = New String() {"Test1", "Test2", "Test3"}
                .ReceiveValue(row)
            End With
        End Sub
    End Class

    Child Form 2:

    Public Class Form3
        Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dg_chat.ColumnCount = 3
            dg_chat.Columns(0).Name = "Column1"
            dg_chat.Columns(1).Name = "Column2"
            dg_chat.Columns(2).Name = "Column3"
        End Sub
    
        Public Sub ReceiveValue(row As String())
            dg_chat.Rows.Add(row)
            Me.Activate()
            Me.Refresh()
        End Sub
    End Class

    And this is the error while trying to update the data from Form2 to form3.

    {"No row can be added to a DataGridView control that does not have columns. Columns must be added first."}


    Coderv9

    Tuesday, October 15, 2019 2:28 PM

Answers

  • Hello,

    This looks like you are targeting the default instance of the form. You need to use the current instance that was created like Dim form3 As New Form3.

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            With Form3
                Dim row As String() = New String() {"Test1", "Test2", "Test3"}
                .ReceiveValue(row)
            End With
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Coderv9 Tuesday, October 15, 2019 3:32 PM
    Tuesday, October 15, 2019 2:44 PM

All replies

  • Hello,

    This looks like you are targeting the default instance of the form. You need to use the current instance that was created like Dim form3 As New Form3.

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            With Form3
                Dim row As String() = New String() {"Test1", "Test2", "Test3"}
                .ReceiveValue(row)
            End With
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Coderv9 Tuesday, October 15, 2019 3:32 PM
    Tuesday, October 15, 2019 2:44 PM
  • Thanks karen,

    I changed the code to below and it's working..

    Public Class Form2
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim row11 As String() = New String() {"Test1", "Test2", "Test3"}
            Dim f3 As Form3 = CType(Application.OpenForms("Form3"), Form3)
            f3.dg_chat.Rows.Add(row11)
        End Sub
    End Class

    • Edited by Coderv9 Tuesday, October 15, 2019 3:32 PM
    Tuesday, October 15, 2019 3:08 PM