[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 520
  • Last Modified:

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

0
Fordraiders
Asked:
Fordraiders
  • 8
  • 4
1 Solution
 
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
 
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now