Master/Detail

S

Serge Poirier

Good Day Folks,

I found a sample on MSDN illustrating Master/Detail. I've
tried it but it doesn't seem to do anything.

Any ideas on how to make it work ?

Thanks in advance
/Serge

<%@ Page Explicit="True" AutoEventWireup="True"
Language="VB"
CompilerOptions='/R:"C:\WINDOWS\Microsoft.NET\Framework\v1.
1.4322\System.Data.OracleClient.dll"' Debug="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
<title>Master Detail</title>
</head>

<script language="VB" runat="server">

private myDataSet As DataSet

Sub Page_Load(sender As Object, e As EventArgs)

MakeDataTables()

End Sub


Private Sub MakeDataTables()
' Run all of the functions.
MakeParentTable()
MakeChildTable()
MakeDataRelation()
BindToDataGrid()
End Sub

Private Sub MakeParentTable()
' Create a new DataTable.
Dim myDataTable As DataTable = new DataTable
("ParentTable")
' Declare variables for DataColumn and DataRow objects.
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create new DataColumn, set DataType, ColumnName and
add to DataTable.
myDataColumn = New DataColumn()
myDataColumn.DataType = System.Type.GetType
("System.Int32")
myDataColumn.ColumnName = "id"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the Column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn()
myDataColumn.DataType = System.Type.GetType
("System.String")
myDataColumn.ColumnName = "ParentItem"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
' Add the column to the table.
myDataTable.Columns.Add(myDataColumn)

' Make the ID column the primary key column.
Dim PrimaryKeyColumns(0) As DataColumn
PrimaryKeyColumns(0)= myDataTable.Columns("id")
myDataTable.PrimaryKey = PrimaryKeyColumns

' Instantiate the DataSet variable.
myDataSet = New DataSet()
' Add the new DataTable to the DataSet.
myDataSet.Tables.Add(myDataTable)

' Create three new DataRow objects and add them to the
DataTable
Dim i As Integer
For i = 0 to 2
myDataRow = myDataTable.NewRow()
myDataRow("id") = i
myDataRow("ParentItem") = "ParentItem " + i.ToString
()
myDataTable.Rows.Add(myDataRow)
Next i
End Sub

Private Sub MakeChildTable()
' Create a new DataTable.
Dim myDataTable As DataTable = New DataTable
("childTable")
Dim myDataColumn As DataColumn
Dim myDataRow As DataRow

' Create first column and add to the DataTable.
myDataColumn = New DataColumn()
myDataColumn.DataType= System.Type.GetType
("System.Int32")
myDataColumn.ColumnName = "ChildID"
myDataColumn.AutoIncrement = True
myDataColumn.Caption = "ID"
myDataColumn.ReadOnly = True
myDataColumn.Unique = True
' Add the column to the DataColumnCollection.
myDataTable.Columns.Add(myDataColumn)

' Create second column.
myDataColumn = New DataColumn()
myDataColumn.DataType= System.Type.GetType
("System.String")
myDataColumn.ColumnName = "ChildItem"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ChildItem"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
myDataTable.Columns.Add(myDataColumn)

' Create third column.
myDataColumn = New DataColumn()
myDataColumn.DataType= System.Type.GetType
("System.Int32")
myDataColumn.ColumnName = "ParentID"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "ParentID"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
myDataTable.Columns.Add(myDataColumn)

myDataSet.Tables.Add(myDataTable)
' Create three sets of DataRow objects, five rows
each, and add to DataTable.
Dim i As Integer
For i = 0 to 4
myDataRow = myDataTable.NewRow()
myDataRow("childID") = i
myDataRow("ChildItem") = "Item " + i.ToString()
myDataRow("ParentID") = 0
myDataTable.Rows.Add(myDataRow)
Next i
For i = 0 to 4
myDataRow = myDataTable.NewRow()
myDataRow("childID") = i + 5
myDataRow("ChildItem") = "Item " + i.ToString()
myDataRow("ParentID") = 1
myDataTable.Rows.Add(myDataRow)
Next i
For i = 0 to 4
myDataRow = myDataTable.NewRow()
myDataRow("childID") = i + 10
myDataRow("ChildItem") = "Item " + i.ToString()
myDataRow("ParentID") = 2
myDataTable.Rows.Add(myDataRow)
Next i
End Sub

Private Sub MakeDataRelation()
' DataRelation requires two DataColumn (parent and
child) and a name.
Dim myDataRelation As DataRelation
Dim parentColumn As DataColumn
Dim childColumn As DataColumn
parentColumn = myDataSet.Tables("ParentTable").Columns
("id")
childColumn = myDataSet.Tables("ChildTable").Columns
("ParentID")
myDataRelation = new DataRelation("parent2Child",
parentColumn, childColumn)
myDataSet.Tables("ChildTable").ParentRelations.Add
(myDataRelation)
End Sub

Private Sub BindToDataGrid()
' Instruct the DataGrid to bind to the DataSet, with
the
' ParentTable as the topmost DataTable.
'DataGrid1.SetDataBinding(myDataSet,"ParentTable")
DataGrid1.DataSource=myDataSet
DataGrid1.DataMember="ParentTable"
DataGrid1.DataBind()
End Sub

</script>

<body text="black" bgcolor="" link="blue" vlink="blue"
alink="blue" leftmargin="0" topmargin="0" marginheight="0"
marginwidth="0">

<center><font size="+2">Master/Detail</font></center>

<form id="EmployeeTableForm" runat="server">

<asp:DataGrid id="DataGrid1" runat="server" />

</form>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top