none
How to pass data from datagridview to textbox of another form RRS feed

  • Question

  • I have the the following:

    Main Form

          Child Form : Textbox and button

                   New Form : DataGridView

         

    Now, what I want is when I load the main form and I call the child form and click button on child form to load another new form where my datagrid can be found, how would I get the data on form with datagrid view and place it on the textbox on my child form.

    Can someone help me on this. Thank you for your response


    Wednesday, June 28, 2017 7:53 AM

Answers

All replies

  • how would I get the data on form with datagrid view and place it on the textbox on my child form.

    The form that is being created needs a reference to the instance of the form that is creating it.  That means it can access properties (fields, controls) of that form for updating as required using that reference.   See here for an example:

    https://vbdotnetblog.wordpress.com/forms/where-did-i-come-from/
    https://vbdotnetblog.wordpress.com/forms/pushing-results-back-to-the-main-form/


    • Edited by Acamar Wednesday, June 28, 2017 8:08 AM fmt
    • Marked as answer by IAN_code Thursday, June 29, 2017 9:00 AM
    Wednesday, June 28, 2017 8:07 AM
  • hello thank you for your response, my child form was within the panel..how can i code it?
    Wednesday, June 28, 2017 8:54 AM
  • Friend can you show me the correct code? Thanks

    Wednesday, June 28, 2017 9:11 AM
  • hello thank you for your response, my child form was within the panel..how can i code it?
    That should make no difference.  Just choose one of the examples provided and adapt it to your situation.  If it has an error or doesn't do what you want, post the code.
    Wednesday, June 28, 2017 9:50 AM
  •  how would I get the data on form with datagrid view and place it on the textbox on my child form.

    Can someone help me on this. Thank you for your response


    Hi lAN_code,

    You want to put what value in datagridview in the TextBox, can you post some code about your question here?

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, June 28, 2017 9:57 AM
    Moderator
  • Here is a working example (I did the first reverse from what you asked while the last is what you want)

    Make sure to read comments within the code

    Main Form

    ''' <summary>
    ''' Shows two different methods for adding data to a unbound DataGridView
    ''' on a child form. An unbound DataGridView is used as you didn't indicate
    ''' anything about the DataGridView.DataSource being set.
    ''' 
    ''' In Button1, we create, show and add a new row to the DataGridView via
    ''' ShowDialog which means you can't click back to Form1 without closing
    ''' Form2.
    ''' 
    ''' Button2 is a bit more code as you can go back and forth between the two
    ''' forms. Note the code in DoFormWork is needed as we must take into consideration
    ''' you might close the child form thus we need to re-create and if you did
    ''' not close the form we use the same instance of that form
    ''' </summary>
    Public Class Form1
        Private mChildForm As Form2
        Private mFirstTime As Boolean = True
        ''' <summary>
        ''' Create new instance of form2, add a row in the DataGridView
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As New Form2
            Try
                f.DataGridView1.Rows.Add(New Object() {TextBox1.Text})
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            DoFormWork(TextBox1.Text)
        End Sub
        ''' <summary>
        ''' Create an new instance of Form2 if not already showing
        ''' Add a row with a value
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If mChildForm IsNot Nothing Then
                mChildForm.Dispose()
            Else
                '  mChildForm is Nothing
            End If
        End Sub
        Private Sub DoFormWork(ByVal Data As String)
            If Not My.Application.IsFormOpen("Form2") Then
                mChildForm = New Form2
                mChildForm.Owner = Me
                mFirstTime = True
            End If
            mChildForm.Show()
            If mFirstTime Then
                mChildForm.Location = New Point(Me.Left + Me.Width, Top)
                mFirstTime = False
            End If
    
            mChildForm.DataGridView1.Rows.Add(New Object() {Data})
    
        End Sub
    
        Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
            If Not mFirstTime Then
                mChildForm.Location = New Point(Left + Width, Top)
            End If
        End Sub
    End Class
    '
    ' The following will work here but should be placed into a new file e.g.
    ' perhaps call it ApplicationUtils.vb (as a class file)
    '
    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Determines if the a form is open
            ''' </summary>
            ''' <param name="sender">Name of form</param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function IsFormOpen(ByVal sender As String) As Boolean
                Return ((From f In Application.OpenForms.Cast(Of Form)() Where f.Name.Equals(sender) Select f.Name).ToList.Count > 0)
            End Function
        End Class
    End Namespace

    Child form, zero code. I created the DataGridView with one column in the IDE.

    In action screen shot via Button2

    Note that moving Form1 with Form2 open, Form2 follows form 1.

    Let's reverse the operation, this time Form2 is the main form, form1 is the child form.

    Public Class Form2
        Private mChildForm As Form1
        Private mFirstTime As Boolean = True
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DoFormWork(TextBox1.Text)
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim f As New Form1
            Try
                f.DataGridView1.Rows.Add(New Object() {TextBox1.Text})
                f.ShowDialog()
            Finally
                f.Dispose()
            End Try
        End Sub
        Private Sub DoFormWork(ByVal Data As String)
            If Not My.Application.IsFormOpen("Form1") Then
                mChildForm = New Form1
                mChildForm.Owner = Me
                mFirstTime = True
            End If
            mChildForm.Show()
            If mFirstTime Then
                mChildForm.Location = New Point(Me.Left + Me.Width, Top)
                mFirstTime = False
            End If
    
            mChildForm.DataGridView1.Rows.Add(New Object() {Data})
    
        End Sub
    
    End Class
    
    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Determines if the a form is open
            ''' </summary>
            ''' <param name="sender">Name of form</param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Function IsFormOpen(ByVal sender As String) As Boolean
                Return ((From f In Application.OpenForms.Cast(Of Form)() Where f.Name.Equals(sender) Select f.Name).ToList.Count > 0)
            End Function
        End Class
    End Namespace
    So in the first example, Form1 has been setup as the main form while in the second example Form2 has been setup as the main form.


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Wednesday, June 28, 2017 10:09 AM
    Moderator
  • Hello again, the sample that you have given was working if i have only one column but if i have several columns to be display on different textboxes there's an error. Take a look on my code. This code is working on one column. How about if several columns. 

    Main form: CustomerProfile

     Dim frm As Terms = New Terms
            frm.Owner = Me
            frm.Show()

    Child form: SalesAgent

            

    Dim MyParent As CustomerProfile
            MyParent = DirectCast(Me.Owner, CustomerProfile)
            DataGridView1.Columns(2).Name = "client"
            MyParent.TextBox8_aname.Text = "client"

            MyParent.TextBox7_acode.Text = (Me.DataGridView1.SelectedCell(2).Value.ToString)
            MyParent.TextBox8_aname.Text = (Me.DataGridView1.SelectedCell(3).Value.ToString)

            Me.Close()

    Thank you again for your response.

    Thursday, June 29, 2017 2:10 AM
  • Please disregard my first message. Kindly refer on the second one that I have sent

    Thursday, June 29, 2017 2:11 AM
  • Hello again, the sample that you have given was working if i have only one column but if i have several columns to be display on different textboxes there's an error. Take a look on my code. This code is working on one column. How about if several columns. 

    Main form: CustomerProfile

     Dim frm As Terms = New Terms
            frm.Owner = Me
            frm.Show()

    Child form: SalesAgent

            

    Dim MyParent As CustomerProfile
            MyParent = DirectCast(Me.Owner, CustomerProfile)
       
            MyParent.TextBox7_acode.Text = (Me.DataGridView1.SelectedCell(2).Value.ToString)
            MyParent.TextBox8_aname.Text = (Me.DataGridView1.SelectedCell(3).Value.ToString)

            Me.Close()

    Thank you again for your response.

    Thursday, June 29, 2017 2:11 AM
  • Hello again, the sample that you have given was working if i have only one column but if i have several columns to be display on different textboxes there's an error.

    What is the error message, what line of code does it refer to, and what are the values of the variables in that line of code?

    The error message usually tells you what the problem is and how to fix it.

    Thursday, June 29, 2017 2:36 AM
  • Thank you very much for your quick response. I analyze it already. My code is now working. Thank you for your patience. I have several questions later. I am a newbie here. God bless:)
    Thursday, June 29, 2017 3:28 AM
  • Thank you very much for your quick response.
    If one or more of the above responses assisted you in solving the problem then they should be marked as answers.  This is to assist others who may have a similar problem.
    • Marked as answer by IAN_code Thursday, June 29, 2017 9:00 AM
    Thursday, June 29, 2017 4:22 AM