view data from two tables into one DataGridView

girl333

New member
Joined
Sep 30, 2012
Messages
4
Programming Experience
Beginner
i want to view data from two tables into one DataGridView like this picture

c1.jpg

i work on visual basic.Net 2005

my database is student_database

we have 2 tables : the frst one is student and it has 2 columns number1,name1

the second one is phone and it has 2 columns number1, phonenumber

this is my form


c2.JPG

this is my code



VB.NET:
Imports System.Data 
Imports System.Data.OleDb 
Public Class تجربة 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim con As OleDbConnection 
        Dim myds As New DataSet() 

        Dim sql0 As String = "select number1,name1 from student" 
        Dim sql1 As String = "select number1,phonenumber from phone" 

        con = New OleDbConnection("Provider=Microsoft.ace.OLEDB.12.0;Data Source=student_database.accdb")
 
        Try 
            Dim dastudent As New OleDb.OleDbDataAdapter(sql0, con) 
            Dim daphone As New OleDb.OleDbDataAdapter(sql1, con) 

            dastudent.Fill(myds, "student") 
            daphone.Fill(myds, "phone") 

            Dim r As DataRelation = New DataRelation("student-phone", myds.Tables("student").Columns("number1"), myds.Tables("phone").Columns("number1"), True)
             myds.Relations.Add(r) 
             
            DataGridView1.DataSource = myds.Tables("student") 

        Catch el As OleDbException 
            Label1.Text = el.Message 
        End Try 

    End Sub 

End Class




when i run this program, just student table displayed

and i want two tables to be displayed like the first picture

can anyone help me and i am sorry for my language :)
 
Last edited:
The DataGridView doesn't support that out of the box and would require some heavy customisation to do so. Generally people will use two DataGridViews bound in a master/detail relationship or else use the old DataGrid control. Even it doesn't support the configuration you've shown, but it will allow navigation between related tables within the one grid.
 
thank u very much

i removed DataGridView1 from my form and i inserted DataGrid1 to my form

also i replaced this row of code

DataGridView1.DataSource = myds.Tables("student")

by this row

DataGrid1.DataSource = myds.Tables("student")

then i got two tables into one DataGrid :)
 
Thank you both, Girl333: the code seems to display only one table, no?
I haven't used a DataGrid for a long time but, if I remember correctly, you should be able to assign the DataSet to the DataSource of the DataGrid and then navigate from DataTable to DataTable within the grid.
 
I am dealing with VB.NET for 6 months almost and I am looking this stuff before, I thought it it impossible. Did you really get the solution same as showed above?
 
I am dealing with VB.NET for 6 months almost and I am looking this stuff before, I thought it it impossible. Did you really get the solution same as showed above?

you can test my program
 

Attachments

  • dispaly two tables into one DataGrid.zip
    47.6 KB · Views: 104
Last edited by a moderator:
You're so great my friend. Thumbs up! Is it possible in listview? Doing some expand and collapse function? Let me know if yes.
 
where should I add dataGrid from in VS 2008 ? I see only dataGridView in the toolbox.
It's not in the Toolbox by default so you have to add it to the Toolbox first and then you can add it from the Toolbox to a form.
 
Back
Top