6 Replies - 2321 Views - Last Post: 21 April 2013 - 06:22 PM Rate Topic: -----

#1 tommywhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 13

Get datagridview row value from form2 and put in textbox of form1

Posted 20 April 2013 - 08:42 AM

I have a textbox and a button.

When the button is clicked it shows a DataGridView which outputs an ID and Name in a separate window (form). These are loaded with select statement from ms-access.

I would like that when I double click on any of the DataGridView rows, the ID is transferred to the textbox which is found in the other window (form).

I tried something like this but no success:
  • Form2.TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString

May please someone tell me what should I have on both form as code please?
(new to vb.net so please bear with me and unfortunately I cannot find help)

Is This A Good Question/Topic? 0
  • +

Replies To: Get datagridview row value from form2 and put in textbox of form1

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,573
  • Joined: 12-December 12

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 20 April 2013 - 09:11 AM

[This is, as you've said yourself, VB.NET, not VB6, so it is in the wrong forum. It will probably be moved for you at some point.]

Do you receive any error messages? If so, what are they.

Use a MessageBox to confirm that it reads the value correctly:

MessageBox.Show(DataGridView1.CurrentRow.Cells(1).Value.ToString)

Was This Post Helpful? 0
  • +
  • -

#3 tommywhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 13

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 20 April 2013 - 11:40 AM

Yes it is alerting the text i need but it is not changing the textbox1 field text. Maybe i need to set the textbox1 as a variable or something in form1? Dunno :S
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,573
  • Joined: 12-December 12

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 20 April 2013 - 11:53 AM

View Posttommywhite, on 20 April 2013 - 06:40 PM, said:

Yes it is alerting the text i need but it is not changing the textbox1 field text. Maybe i need to set the textbox1 as a variable or something in form1? Dunno :S

You still haven't confirmed if you receive error messages?

The following simple code works, and I assume you are doing something similar?

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim frm2 As New Form2
        frm2.Show()
        frm2.TextBox1.Text = "Hello from Form1"
    End Sub

You are not just using the static object/reference Form2..?

BTW This is not a good way to proceed as it makes the two forms too tightly-coupled. Subscribing to events is the way to approach this.
Was This Post Helpful? 0
  • +
  • -

#5 tommywhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 13

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 21 April 2013 - 12:15 AM

I think you did not understand what is my question.

Form1 - has button1 and textbox1 which when the button is pressed it will show Form2
Form2 - has a datagridview which shows all the rows and columns. All I would like to have is when I double click on the datagridview is to change the Form1's textbox1 into that rows ID (which will be in the first column)

P.S. When I alerted what is the ID I got the ID outputted in a box, fair enough. So getting the value of the ID column is working. The problem is that the textbox1 is not changing to the ID. thanks for your help
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,573
  • Joined: 12-December 12

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 21 April 2013 - 01:49 AM

The following code would be in the first form:

    Private WithEvents frm2 As Form2

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles Button3.Click
        frm2 = New Form2
        AddHandler frm2.DataGridView1.DoubleClick, AddressOf GetDetail
        frm2.Show()
    End Sub

    Private Sub GetDetail(ByVal sender As Object, ByVal e As EventArgs)
        TextBox1.Text = CType(sender, DataGridView).CurrentRow.Cells(1).Value.ToString
    End Sub

Was This Post Helpful? 1
  • +
  • -

#7 tommywhite  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-April 13

Re: Get datagridview row value from form2 and put in textbox of form1

Posted 21 April 2013 - 06:22 PM

Thank mate it worked, you have no idea how much I appreciate :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1