467,081 Members | 969 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

how to add a column in a master detail realtionship using datagrid controls?

I built a form that displays a master-detail relation ship, now i need to
add a column to the datagrid displaying the master data, this column
corresponds to the text name of a column stored in the master table as an
integer (the foreign key, i need to display the text name of the foreign key
stored in the third table).

Do i have to create another datatable in the dataset im using that includes
all the information I need?
Change the Sql statement to include that information in the master table?
....

what is the best way to do it? I would prefer that my dataset represents the
database structure table by table.

Nov 22 '05 #1
  • viewed: 1391
Share:
1 Reply
Juan,

I made a sample for the language.vb newsgroup for that, I changed it a
little bit, and maybe it fits your problem

\\\
'The first click on the button shows one datagrid
'The second click shows the same info with two datagrid
'To make it nice a lot of other code is needed by instance
datagridtablestyles
'and columnstyles
Private ds As New DataSet
Private dtCountries As DataTable
Private dtVBReg As DataTable
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateTables()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Static pushed As Integer = 1
Select Case pushed
Case 1
dtVBReg.Columns.Add("MyNewColumn", GetType(System.Boolean),
"Country = 'US'")
Label1.Text = "Relation One Datagrid"
Dim drel As New DataRelation _
("Regulars", ds.Tables("Countries").Columns("Country"), _
ds.Tables("Persons").Columns("Country"))
ds.Relations.Add(drel)
DataGrid1.DataSource = dtCountries
DataGrid1.Expand(-1)
DataGrid1.ReadOnly = True
Case 2
dtVBReg.Columns.Add("MyNewColumn", GetType(System.Boolean),
"Country = 'US'")
ds.Relations.RemoveAt(0)
Label1.Text = "Relation two Datatgrids"
Dim drel As New DataRelation _
("Regulars", ds.Tables("Countries").Columns("Country"), _
ds.Tables("Persons").Columns("Country"))
ds.Relations.Add(drel)
DataGrid1.SetDataBinding(ds, "Countries")
DataGrid2.SetDataBinding(ds, "Countries.Regulars")
DataGrid1.AllowNavigation = False
DataGrid2.AllowNavigation = False
DataGrid2.ReadOnly = True

End Select
pushed += 1
End Sub
Private Sub CreateTables()
dtVBreg = New DataTable("Persons")
dtVBreg.Columns.Add("Name")
dtVBreg.Columns.Add("Country")
For i As Integer = 0 To 7
dtVBreg.Rows.Add(dtVBreg.NewRow)
Next
dtVBreg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU"}
dtVBreg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US"}
dtVBreg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US"}
dtVBreg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", "US"}
dtVBreg.Rows(4).ItemArray = New Object() _
{"Terry Burns", "EU"}
dtVBreg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", "US"}
dtVBreg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", "EU"}
dtCountries = New DataTable("Countries")
dtCountries.Columns.Add("Country")
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountries.NewRow
dr(0) = i.ToString
dtCountries.Rows.Add(dr)
Next
dtCountries.Rows(0)(0) = "EU"
dtCountries.Rows(1)(0) = "US"
ds.Tables.Add(dtVBreg)
ds.Tables.Add(dtCountries)
End Sub
////

I hope this helps?

Cor

"Juan" <ju*****************@ANTISPAMhotmail.com>
I built a form that displays a master-detail relation ship, now i need to
add a column to the datagrid displaying the master data, this column
corresponds to the text name of a column stored in the master table as an
integer (the foreign key, i need to display the text name of the foreign
key
stored in the third table).

Do i have to create another datatable in the dataset im using that
includes
all the information I need?
Change the Sql statement to include that information in the master table?
...

what is the best way to do it? I would prefer that my dataset represents
the
database structure table by table.


Nov 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Marcel | last post: by
6 posts views Thread by Beren | last post: by
3 posts views Thread by jason | last post: by
8 posts views Thread by Doug Bell | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.