Copying data from datagridview to textbox on another form

vb.net 2008

What I have:
2 forms
"frmsearch" and  "frmMainDataEntry"
On "frmsearch"
datagridview1

On "frmMainDataEntry"
Textbox1
textbox2
textbox3

What I need:
1. select a row in the datagrid.
2. Hit a command button.
3. Check to make sure  "frmMainDataEntry" IS OPEN ?  If Not then OPEN IT. If it is open then go ahead and post.
3. from  "frmSearch" Copy data from datagrid to "frmMainDataEntry"  texboxes
4. Alignment is:
            Me.TextBox1.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            Me.TextBox2.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            Me.TextBox3.Text = Me.DataGridView1.CurrentRow.Cells(2).Value

Thanks
fordraiders

LVL 3
FordraidersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jorge PaulinoIT Pro/DeveloperCommented:
You can do something like this:
        Dim frm As Form2 = Nothing
        Dim f As Form = My.Application.OpenForms("Form2")

        If f IsNot Nothing Then
            frm = f
            frm.TextBox1.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.TextBox2.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.TextBox3.Text = Me.DataGridView1.CurrentRow.Cells(2).Value
            frm.Activate()
        Else
            frm = New Form2
            frm.TextBox1.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.TextBox2.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.TextBox3.Text = Me.DataGridView1.CurrentRow.Cells(2).Value
            frm.Show()
        End If

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FordraidersAuthor Commented:
iPauline,
Works  fine as far as posting.
But I need the form to get the focus back. It will activate the form, but will not set focus back to the form.

If f IsNot Nothing Then
            frm = f
            frm.txtCmfrnum.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.cboMfgnames.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.txtSku.Text = Me.DataGridView1.CurrentRow.Cells(2).Value
            frm.Activate()
            frm.txtCmfrnum.Select()    <--- I tried this but t I still have to get to the form manually.

Thanks
fordraiders
0
FordraidersAuthor Commented:
This will work as long as someone does not minimize the form.
  frm.Activate()
  frm.Show()
  frm.Select()
  frm.txtCmfrnum.Select()
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Jorge PaulinoIT Pro/DeveloperCommented:
Ok, then change to this (the first part):
            frm = f
            frm.TextBox1.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.TextBox2.Text = Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.TextBox3.Text = Me.DataGridView1.CurrentRow.Cells(2).Value
            frm.WindowState = FormWindowState.Normal
            frm.BringToFront()
 
0
CodeCruiserCommented:
By the way

Use
frm.txtCmfrnum.Focus()

instead of
frm.txtCmfrnum.Select()
0
FordraidersAuthor Commented:
ipauline... Thanks

aaah.. keep looking for  "setfocus"...vba stuff
0
FordraidersAuthor Commented:
ipauline,
This is not working, when the form is not open...
and then trying to ppost data after openming the form..
Works fine if the form is already open,

The "else" will open the form but the data i'm bringing  in from the datagrid is not getting posted ?



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim frm As frmiCart = Nothing
        Dim f As Form = My.Application.OpenForms("frmiCart")

        If f IsNot Nothing Then
            ' FORM IS ALREADY OPEN HERE
            frm = f
            frm.Activate()
            frm.Show()
            frm.txtSku.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.txtXref.Text = "F" 'Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.txtCdescription.Text = Me.DataGridView1.CurrentRow.Cells(10).Value
            frm.txtSegment.Text = "ExtN"
            frm.WindowState = FormWindowState.Maximized
            frm.BringToFront()
            frm.txtCmfrnum.Focus()
        Else                  '    < --------- this part is not working ---------
            ' FORM IS NOT ALREADY OPEN HERE
            frm = New frmiCart
            frm.txtXref.Text = "F" 'Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.txtCdescription.Text = Me.DataGridView1.CurrentRow.Cells(10).Value
            frm.txtSegment.Text = "ExtN"
            frm.txtSku.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.Activate()
            frm.Show()
            frm.WindowState = FormWindowState.Maximized
            frm.BringToFront()
         frm.txtCmfrnum.Focus()
            frm.txtCmfrnum.Select()
        End If
    End Sub

0
Jorge PaulinoIT Pro/DeveloperCommented:
What is not working ? What error do you have?
0
FordraidersAuthor Commented:
no error, just not posting data...
0
FordraidersAuthor Commented:
Else                  '    < --------- this part is not working ---------
            ' FORM IS NOT ALREADY OPEN HERE
            frm = New frmiCart
            frm.txtXref.Text = "F" 'Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.txtCdescription.Text = Me.DataGridView1.CurrentRow.Cells(10).Value
            frm.txtSegment.Text = "ExtN"
            frm.txtSku.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.Activate()
            frm.Show()
            frm.WindowState = FormWindowState.Maximized
            frm.BringToFront()
         frm.txtCmfrnum.Focus()
            frm.txtCmfrnum.Select()
0
FordraidersAuthor Commented:
did some swithcing around on the form opening up sequence.
this works
Else
            ' FORM IS NOT ALREADY OPEN HERE
            frm = New frmiCart
            frm.Activate()
            frm.Show()
            frm.txtXref.Text = "F" 'Me.DataGridView1.CurrentRow.Cells(1).Value
            frm.txtCdescription.Text = Me.DataGridView1.CurrentRow.Cells(10).Value
            frm.txtSegment.Text = "ExtN"
            frm.txtSku.Text = Me.DataGridView1.CurrentRow.Cells(0).Value
            frm.BringToFront()
            frm.WindowState = FormWindowState.Maximized
            frm.txtCmfrnum.Focus()
            frm.txtCmfrnum.Select()
0
Jorge PaulinoIT Pro/DeveloperCommented:
You don't need all that ... Focus(), Select(), Activate(), etc
This should be enough
frm.Show()
frm.WindowState = FormWindowState.Maximized  
frm.BringToFront()
frm.txtCmfrnum.Select()
 
0
FordraidersAuthor Commented:
ok...Thanks very much again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.