Link to home
Start Free TrialLog in
Avatar of Amr Aly
Amr AlyFlag for Egypt

asked on

how can mouse click on data grid view display the content of a table in the database that didn't link to it

Hello everybody, I'm a beginner of VB.net and I face this problem ....
how data grid view on click display a data of another table that doesn't link to it..the 2 pictures indicate the problem obviously.
i'm using MS Access 2013 as a database
thanks in advance.................................
2.png
3.png
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

I'm assuming some value, or collection of values, in the datagridview could give provide you with a "handle" on the desired data in the other table? If so, you can query data from the database and fill those boxes from code:

Using con As New SQLConnection("Your connect string")
  con.Open
  Using cmd As New SQLCommand
    cmd.Connection = con
    cmd.commandtext = "SELECT * FROM SomeTable WHERE SomeField=" & YourDataGridView.CurrentRow.Cells(0).Value
    Using dt As New Datatable
      dt.Load(cmd.executeReader)
      if dt.Rows.Count > 0 Then
        tx1.Text = dt.rows(0).item("Col1")
        tx2.Text = dt.rows(0).item("Col2")
      End if
    End Using
  End Using

End Using
Avatar of Amr Aly

ASKER

thank you sir for your fast response, But my code link Access 2013 to my project confuses my and differ from yours ..
Please take a look to the attached text file ......
I'm facing lots of problems and i will tell you about it after solving these one. Some problems i solve it myself by deep thinking like saving null in Access database, and i will show you how can i overcome this problem to tell if it true or false, Although My program accept it but i want to be sure   ....
Many Thanks to These Web Site It is so much useful
and Many thanks to you Sir................
My_Code.txt
My apologies - I didn't notice you were using an Access database.

Just use your OLEDBConnection and OLEDBCommand items, and query the Access database for the data you need. You can use that method even if you're using other databound methods (like DataAdapters, etc).
Avatar of Amr Aly

ASKER

you mean that if i write your code above in
" Private Sub dgvVisit_MouseClick(sender As Object, e As MouseEventArgs) Handles dgvVisit.MouseClick "
it will work well.............. i will try and tell you if it is work
I'd probably use a different event, like RowEnter, but you could use MouseClick, or whatever event makes sense to your program.

You will of course have to change Table, Column and Control names to match those in your project, and of course you'll have to provide the correct filter in your query to return the right records.
Avatar of Amr Aly

ASKER

i want every empty text boxes fill with specific data.......... i.e when click a certain Visit (Visit number 1) on the grid view every text boxes with this Visit no(Visit number 1) in the other table appear
Avatar of Amr Aly

ASKER

Unfortunately your code didn't work ..........Message error occur but the program continue with another message error when click on the grid view............"There is no row at position 1"..........  this is the error message occur on grid view click
Then use the RowEnter event. If you do that, however, you should change the code somewhat to refer to the correct row:

 cmd.commandtext = "SELECT * FROM SomeTable WHERE SomeField=" & YourDataGridView.Rows(e.RowIndex).Cells(0).Value
Avatar of Amr Aly

ASKER

I' m so sorry but after many of tries ...... The code gave me the same error  "There is no row at position 1" and "There is no row at position 4"
4.png
Can you show the entire code where you're trying to fill the boxes?
Avatar of Amr Aly

ASKER

Thank you sir i will send it to you
this is the code of form 2 do you want the form 1 also.
Form2.vb
You don't refer to a different ROW, but instead just to the different items in that row (assuming your SELECT statement returns a single row):

txtVisNo.Text = dt.Rows(0).Item("Visit_no")
txtDrug.Text = dt.Rows(0).Item("NameDrug")
txtPlan.Text = dt.Rows(0).Item("NamePlan")
txtDrug1.Text = dt.Rows(0).Item("NameDrug1")
etc etc

This is in the RowEnter event ...
Avatar of Amr Aly

ASKER

you mean that i should do like that....

txtVisNo.Text = dt.Rows(0).Item("Visit_no")
txtDrug.Text = dt.Rows(0).Item("NameDrug")
txtPlan.Text = dt.Rows(0).Item("NamePlan")
txtDrug1.Text = dt.Rows(0).Item("NameDrug1")
etc etc

txtVisNo.Text = dt.Rows(1).Item("Visit_no")
txtDrug.Text = dt.Rows(1).Item("NameDrug")
txtPlan.Text = dt.Rows(1).Item("NamePlan")
txtDrug1.Text = dt.Rows(1).Item("NameDrug1")
etc etc

txtVisNo.Text = dt.Rows(2).Item("Visit_no")
txtDrug.Text = dt.Rows(2).Item("NameDrug")
txtPlan.Text = dt.Rows(2).Item("NamePlan")
txtDrug1.Text = dt.Rows(2).Item("NameDrug1")
etc etc

and so on ..... that is my understanding..... Am i right or not ?
Avatar of Amr Aly

ASKER

The text boxes of drug begin to fill when i click the grid view but with the same error " no number in position 1 " and when i changed the SQL select statement  these messages "object reference not set to an instance to an object " appear and program stop to work after go to the second form when i click the grid view....
forgive me i'm a newbie in vb.net
select_statment.txt
5.png
6.png
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial