Results 1 to 11 of 11

Thread: VB.NET - Show Two tables in one DataGrid

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    VB.NET - Show Two tables in one DataGrid

    Using ADO.NET and Relations . Check the code it's commented .
    Attached Files Attached Files

  2. #2

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I created it with VS.NET 2003 , if you have the previous version , then you need to use the converter under my sig . And the database , I used MS Access XP .

  3. #3
    Junior Member iqueen's Avatar
    Join Date
    Sep 2003
    Posts
    29
    PHP Code:
            'First command for first table          cmd1 = New OleDbCommand          cmd1.Connection = conn          cmd1.CommandText = "SELECT * FROM Tab1"              'Second command for Second table          cmd2 = New OleDbCommand          cmd2.Connection conn          cmd2.CommandText "SELECT * FROM Tab2" 
    why donot
    PHP Code:
    cmd1.CommandText "SELECT * FROM Tab1,Tab2" 

  4. #4
    Junior Member iqueen's Avatar
    Join Date
    Sep 2003
    Posts
    29
    'First command for first table
    cmd1 = New OleDbCommand
    cmd1.Connection = conn
    cmd1.CommandText = "SELECT * FROM Tab1"


    'Second command for Second table
    cmd2 = New OleDbCommand
    cmd2.Connection = conn
    cmd2.CommandText = "SELECT * FROM Tab2"
    why donot u
    cmd1.CommandText = "SELECT * FROM Tab1,Tab2"

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    No , I found it more organized when iterating two separate obj with two different sort of data .

  6. #6
    Lively Member
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    125

    Smile friendly request...oh pretty please!

    interesting program you have there...It might be useful to me but my problem is I can't open it...the computer i'm working on doesn't have a win rar application and I can't install one since this is not mine...I was thinking if u could post the code cause I'm looking for a code in deleting master/detail records. I have one already but it actually deletes the first record in my database tables. Hoping for a most positive reply from u soon!

  7. #7
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    sig of pirate has link to winrar.

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    That's the whole code .
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Path to database
    3.         Dim dbpath As String = Application.StartupPath & "\mydb.mdb"
    4.  
    5.         'Connection obj to database
    6.         Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Jet OLEDB:Database Password=")
    7.  
    8.         'Open the Connetion
    9.         conn.Open()
    10.         'Dataset that holds data in disconnected mode
    11.         Dim ds As New DataSet
    12.  
    13.         'Two commands for two tables (tab1 and tab2)
    14.         Dim cmd1 As OleDbCommand
    15.         Dim cmd2 As OleDbCommand
    16.  
    17.         'Two datapaters to fill the dataset from two tables
    18.         Dim adp1 As OleDbDataAdapter
    19.         Dim adp2 As OleDbDataAdapter
    20.  
    21.         'This handles the relationship between the two columns
    22.         Dim datarelation As DataRelation
    23.         Dim dc1 As DataColumn
    24.         Dim dc2 As DataColumn
    25.  
    26.         'It's not important but gives your code more better way to
    27.         'compare strings between tables
    28.         ds.CaseSensitive = True
    29.  
    30.         'First command for first table
    31.         cmd1 = New OleDbCommand
    32.         cmd1.Connection = conn
    33.         cmd1.CommandText = "SELECT * FROM Tab1"
    34.  
    35.  
    36.         'Second command for Second table
    37.         cmd2 = New OleDbCommand
    38.         cmd2.Connection = conn
    39.         cmd2.CommandText = "SELECT * FROM Tab2"
    40.  
    41.         'Now , we will fill the first table and add it to the dataset
    42.         adp1 = New OleDbDataAdapter
    43.         adp1.SelectCommand = cmd1
    44.         adp1.TableMappings.Add("Table", "Tab1")
    45.         adp1.Fill(ds)
    46.  
    47.  
    48.         'As we did in the previous step , here for the Second table
    49.         adp2 = New OleDbDataAdapter
    50.         adp2.SelectCommand = cmd2
    51.         adp2.TableMappings.Add("Table", "Tab2")
    52.         adp2.Fill(ds)
    53.  
    54.  
    55.  
    56.         dc1 = ds.Tables("Tab1").Columns("ID")
    57.         dc2 = ds.Tables("Tab2").Columns("ID")
    58.  
    59.         'Here we combined two datacolumns to the relations obj
    60.         datarelation = New DataRelation("Tab1andTab2", dc1, dc2)
    61.         ds.Relations.Add(datarelation)
    62.  
    63.         'Simple one , bind the dataset after all operation to the
    64.         'Datagrid
    65.         DataGrid1.DataSource = ds.DefaultViewManager
    66.         'Show the first table in the grid because it's the primary table
    67.         DataGrid1.DataMember = "tab1"
    68.  
    69.         'That's all folks :)
    70.         'Pirate
    71.  
    72.     End Sub

  9. #9
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    Talking VB.NET - Show Two tables in one DataGrid

    Pirate,

    This is a great piece of code. It is really helpful. I've been pulling my hair out trying to do this.

    I have placed your code into another forum Link so that other people can appriecate your hard work.

    Once again thank you for placing this code onto the web.

    SonicBoomAu.

  10. #10
    Lively Member
    Join Date
    Jan 2007
    Posts
    95

    Re: VB.NET - Show Two tables in one DataGrid

    i'm getting a problem...when i update the second table, i give me this error: Syntax error in INSERT INTO statement.
    Last edited by extreme.aly; Aug 17th, 2007 at 09:02 AM.

  11. #11
    Junior Member vks.gautam1's Avatar
    Join Date
    Dec 2008
    Location
    Chandigarh, India
    Posts
    27

    Re: VB.NET - Show Two tables in one DataGrid

    Thanks. Good Tutorial. It is working.
    But what i want is like.
    Select
    t1.*,t2.*
    from
    tab1 as t1
    inner join
    tab2 as t2
    on t1.id=t2.id

    Can we show both tables related data with the help of two data tables in DataGridview.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width