Jul 15th, 2003, 02:02 PM
#1
Thread Starter
Sleep mode
VB.NET - Show Two tables in one DataGrid
Using ADO.NET and Relations . Check the code it's commented .
Attached Files
Jul 15th, 2003, 02:04 PM
#2
Thread Starter
Sleep mode
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 .
Sep 18th, 2003, 08:31 AM
#3
Junior Member
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"
Sep 18th, 2003, 08:32 AM
#4
Junior Member
'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"
Sep 18th, 2003, 09:24 AM
#5
Thread Starter
Sleep mode
No , I found it more organized when iterating two separate obj with two different sort of data .
Aug 18th, 2004, 09:14 PM
#6
Lively Member
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!
Aug 19th, 2004, 10:09 PM
#7
Fanatic Member
sig of pirate has link to winrar.
Aug 20th, 2004, 08:07 AM
#8
Thread Starter
Sleep mode
That's the whole code .
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Path to database
Dim dbpath As String = Application.StartupPath & "\mydb.mdb"
'Connection obj to database
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath & ";Jet OLEDB:Database Password=")
'Open the Connetion
conn.Open()
'Dataset that holds data in disconnected mode
Dim ds As New DataSet
'Two commands for two tables (tab1 and tab2)
Dim cmd1 As OleDbCommand
Dim cmd2 As OleDbCommand
'Two datapaters to fill the dataset from two tables
Dim adp1 As OleDbDataAdapter
Dim adp2 As OleDbDataAdapter
'This handles the relationship between the two columns
Dim datarelation As DataRelation
Dim dc1 As DataColumn
Dim dc2 As DataColumn
'It's not important but gives your code more better way to
'compare strings between tables
ds.CaseSensitive = True
'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"
'Now , we will fill the first table and add it to the dataset
adp1 = New OleDbDataAdapter
adp1.SelectCommand = cmd1
adp1.TableMappings.Add("Table", "Tab1")
adp1.Fill(ds)
'As we did in the previous step , here for the Second table
adp2 = New OleDbDataAdapter
adp2.SelectCommand = cmd2
adp2.TableMappings.Add("Table", "Tab2")
adp2.Fill(ds)
dc1 = ds.Tables("Tab1").Columns("ID")
dc2 = ds.Tables("Tab2").Columns("ID")
'Here we combined two datacolumns to the relations obj
datarelation = New DataRelation("Tab1andTab2", dc1, dc2)
ds.Relations.Add(datarelation)
'Simple one , bind the dataset after all operation to the
'Datagrid
DataGrid1.DataSource = ds.DefaultViewManager
'Show the first table in the grid because it's the primary table
DataGrid1.DataMember = "tab1"
'That's all folks :)
'Pirate
End Sub
Mar 22nd, 2005, 12:04 AM
#9
Lively Member
Aug 17th, 2007, 08:55 AM
#10
Lively Member
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 .
Jan 2nd, 2009, 03:01 AM
#11
Junior Member
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
Forum Rules
Click Here to Expand Forum to Full Width