Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / Visual Basic

DataGrid101: Using Windows.Forms DataGrid

Rate me:
Please Sign up or sign in to vote.
4.70/5 (73 votes)
14 May 2003CPOL8 min read 601.6K   3.2K   147  
Tutorial on the usage of Windows.Forms.DataGrid
'------------------------------------------------------------------------------
' <autogenerated>
'     This code was generated by a tool.
'     Runtime Version: 1.0.3705.0
'
'     Changes to this file may cause incorrect behavior and will be lost if 
'     the code is regenerated.
' </autogenerated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On

Imports System
Imports System.Data
Imports System.Runtime.Serialization
Imports System.Xml


<Serializable(),  _
 System.ComponentModel.DesignerCategoryAttribute("code"),  _
 System.Diagnostics.DebuggerStepThrough(),  _
 System.ComponentModel.ToolboxItem(true)>  _
Public Class CarsDataSet
    Inherits DataSet
    
    Private tableCars As CarsDataTable
    
    Private tableManufacturer As ManufacturerDataTable
    
    Private tableTypes As TypesDataTable
    
    Private relationManufacturerTypes As DataRelation
    
    Private relationTypesCars As DataRelation
    
    Public Sub New()
        MyBase.New
        Me.InitClass
        Dim schemaChangedHandler As System.ComponentModel.CollectionChangeEventHandler = AddressOf Me.SchemaChanged
        AddHandler Me.Tables.CollectionChanged, schemaChangedHandler
        AddHandler Me.Relations.CollectionChanged, schemaChangedHandler
    End Sub
    
    Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New
        Dim strSchema As String = CType(info.GetValue("XmlSchema", GetType(System.String)),String)
        If (Not (strSchema) Is Nothing) Then
            Dim ds As DataSet = New DataSet
            ds.ReadXmlSchema(New XmlTextReader(New System.IO.StringReader(strSchema)))
            If (Not (ds.Tables("Cars")) Is Nothing) Then
                Me.Tables.Add(New CarsDataTable(ds.Tables("Cars")))
            End If
            If (Not (ds.Tables("Manufacturer")) Is Nothing) Then
                Me.Tables.Add(New ManufacturerDataTable(ds.Tables("Manufacturer")))
            End If
            If (Not (ds.Tables("Types")) Is Nothing) Then
                Me.Tables.Add(New TypesDataTable(ds.Tables("Types")))
            End If
            Me.DataSetName = ds.DataSetName
            Me.Prefix = ds.Prefix
            Me.Namespace = ds.Namespace
            Me.Locale = ds.Locale
            Me.CaseSensitive = ds.CaseSensitive
            Me.EnforceConstraints = ds.EnforceConstraints
            Me.Merge(ds, false, System.Data.MissingSchemaAction.Add)
            Me.InitVars
        Else
            Me.InitClass
        End If
        Me.GetSerializationData(info, context)
        Dim schemaChangedHandler As System.ComponentModel.CollectionChangeEventHandler = AddressOf Me.SchemaChanged
        AddHandler Me.Tables.CollectionChanged, schemaChangedHandler
        AddHandler Me.Relations.CollectionChanged, schemaChangedHandler
    End Sub
    
    <System.ComponentModel.Browsable(false),  _
     System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)>  _
    Public ReadOnly Property Cars As CarsDataTable
        Get
            Return Me.tableCars
        End Get
    End Property
    
    <System.ComponentModel.Browsable(false),  _
     System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)>  _
    Public ReadOnly Property Manufacturer As ManufacturerDataTable
        Get
            Return Me.tableManufacturer
        End Get
    End Property
    
    <System.ComponentModel.Browsable(false),  _
     System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)>  _
    Public ReadOnly Property Types As TypesDataTable
        Get
            Return Me.tableTypes
        End Get
    End Property
    
    Public Overrides Function Clone() As DataSet
        Dim cln As CarsDataSet = CType(MyBase.Clone,CarsDataSet)
        cln.InitVars
        Return cln
    End Function
    
    Protected Overrides Function ShouldSerializeTables() As Boolean
        Return false
    End Function
    
    Protected Overrides Function ShouldSerializeRelations() As Boolean
        Return false
    End Function
    
    Protected Overrides Sub ReadXmlSerializable(ByVal reader As XmlReader)
        Me.Reset
        Dim ds As DataSet = New DataSet
        ds.ReadXml(reader)
        If (Not (ds.Tables("Cars")) Is Nothing) Then
            Me.Tables.Add(New CarsDataTable(ds.Tables("Cars")))
        End If
        If (Not (ds.Tables("Manufacturer")) Is Nothing) Then
            Me.Tables.Add(New ManufacturerDataTable(ds.Tables("Manufacturer")))
        End If
        If (Not (ds.Tables("Types")) Is Nothing) Then
            Me.Tables.Add(New TypesDataTable(ds.Tables("Types")))
        End If
        Me.DataSetName = ds.DataSetName
        Me.Prefix = ds.Prefix
        Me.Namespace = ds.Namespace
        Me.Locale = ds.Locale
        Me.CaseSensitive = ds.CaseSensitive
        Me.EnforceConstraints = ds.EnforceConstraints
        Me.Merge(ds, false, System.Data.MissingSchemaAction.Add)
        Me.InitVars
    End Sub
    
    Protected Overrides Function GetSchemaSerializable() As System.Xml.Schema.XmlSchema
        Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream
        Me.WriteXmlSchema(New XmlTextWriter(stream, Nothing))
        stream.Position = 0
        Return System.Xml.Schema.XmlSchema.Read(New XmlTextReader(stream), Nothing)
    End Function
    
    Friend Sub InitVars()
        Me.tableCars = CType(Me.Tables("Cars"),CarsDataTable)
        If (Not (Me.tableCars) Is Nothing) Then
            Me.tableCars.InitVars
        End If
        Me.tableManufacturer = CType(Me.Tables("Manufacturer"),ManufacturerDataTable)
        If (Not (Me.tableManufacturer) Is Nothing) Then
            Me.tableManufacturer.InitVars
        End If
        Me.tableTypes = CType(Me.Tables("Types"),TypesDataTable)
        If (Not (Me.tableTypes) Is Nothing) Then
            Me.tableTypes.InitVars
        End If
        Me.relationManufacturerTypes = Me.Relations("ManufacturerTypes")
        Me.relationTypesCars = Me.Relations("TypesCars")
    End Sub
    
    Private Sub InitClass()
        Me.DataSetName = "CarsDataSet"
        Me.Prefix = ""
        Me.Namespace = "http://www.tempuri.org/CarsDataSet.xsd"
        Me.Locale = New System.Globalization.CultureInfo("he-IL")
        Me.CaseSensitive = false
        Me.EnforceConstraints = true
        Me.tableCars = New CarsDataTable
        Me.Tables.Add(Me.tableCars)
        Me.tableManufacturer = New ManufacturerDataTable
        Me.Tables.Add(Me.tableManufacturer)
        Me.tableTypes = New TypesDataTable
        Me.Tables.Add(Me.tableTypes)
        Dim fkc As ForeignKeyConstraint
        fkc = New ForeignKeyConstraint("TypesCars", New DataColumn() {Me.tableTypes.typeIDColumn}, New DataColumn() {Me.tableCars.typeIDColumn})
        Me.tableCars.Constraints.Add(fkc)
        fkc.AcceptRejectRule = AcceptRejectRule.None
        fkc.DeleteRule = Rule.Cascade
        fkc.UpdateRule = Rule.Cascade
        fkc = New ForeignKeyConstraint("ManufacturerTypes", New DataColumn() {Me.tableManufacturer.ManufacturerIDColumn}, New DataColumn() {Me.tableTypes.manufacturerIDColumn})
        Me.tableTypes.Constraints.Add(fkc)
        fkc.AcceptRejectRule = AcceptRejectRule.None
        fkc.DeleteRule = Rule.Cascade
        fkc.UpdateRule = Rule.Cascade
        Me.relationManufacturerTypes = New DataRelation("ManufacturerTypes", New DataColumn() {Me.tableManufacturer.ManufacturerIDColumn}, New DataColumn() {Me.tableTypes.manufacturerIDColumn}, false)
        Me.Relations.Add(Me.relationManufacturerTypes)
        Me.relationTypesCars = New DataRelation("TypesCars", New DataColumn() {Me.tableTypes.typeIDColumn}, New DataColumn() {Me.tableCars.typeIDColumn}, false)
        Me.Relations.Add(Me.relationTypesCars)
    End Sub
    
    Private Function ShouldSerializeCars() As Boolean
        Return false
    End Function
    
    Private Function ShouldSerializeManufacturer() As Boolean
        Return false
    End Function
    
    Private Function ShouldSerializeTypes() As Boolean
        Return false
    End Function
    
    Private Sub SchemaChanged(ByVal sender As Object, ByVal e As System.ComponentModel.CollectionChangeEventArgs)
        If (e.Action = System.ComponentModel.CollectionChangeAction.Remove) Then
            Me.InitVars
        End If
    End Sub
    
    Public Delegate Sub CarsRowChangeEventHandler(ByVal sender As Object, ByVal e As CarsRowChangeEvent)
    
    Public Delegate Sub ManufacturerRowChangeEventHandler(ByVal sender As Object, ByVal e As ManufacturerRowChangeEvent)
    
    Public Delegate Sub TypesRowChangeEventHandler(ByVal sender As Object, ByVal e As TypesRowChangeEvent)
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class CarsDataTable
        Inherits DataTable
        Implements System.Collections.IEnumerable
        
        Private columnlicense As DataColumn
        
        Private columntypeID As DataColumn
        
        Friend Sub New()
            MyBase.New("Cars")
            Me.InitClass
        End Sub
        
        Friend Sub New(ByVal table As DataTable)
            MyBase.New(table.TableName)
            If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
                Me.CaseSensitive = table.CaseSensitive
            End If
            If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
                Me.Locale = table.Locale
            End If
            If (table.Namespace <> table.DataSet.Namespace) Then
                Me.Namespace = table.Namespace
            End If
            Me.Prefix = table.Prefix
            Me.MinimumCapacity = table.MinimumCapacity
            Me.DisplayExpression = table.DisplayExpression
        End Sub
        
        <System.ComponentModel.Browsable(false)>  _
        Public ReadOnly Property Count As Integer
            Get
                Return Me.Rows.Count
            End Get
        End Property
        
        Friend ReadOnly Property licenseColumn As DataColumn
            Get
                Return Me.columnlicense
            End Get
        End Property
        
        Friend ReadOnly Property typeIDColumn As DataColumn
            Get
                Return Me.columntypeID
            End Get
        End Property
        
        Public Default ReadOnly Property Item(ByVal index As Integer) As CarsRow
            Get
                Return CType(Me.Rows(index),CarsRow)
            End Get
        End Property
        
        Public Event CarsRowChanged As CarsRowChangeEventHandler
        
        Public Event CarsRowChanging As CarsRowChangeEventHandler
        
        Public Event CarsRowDeleted As CarsRowChangeEventHandler
        
        Public Event CarsRowDeleting As CarsRowChangeEventHandler
        
        Public Overloads Sub AddCarsRow(ByVal row As CarsRow)
            Me.Rows.Add(row)
        End Sub
        
        Public Overloads Function AddCarsRow(ByVal license As String, ByVal parentTypesRowByTypesCars As TypesRow) As CarsRow
            Dim rowCarsRow As CarsRow = CType(Me.NewRow,CarsRow)
            rowCarsRow.ItemArray = New Object() {license, parentTypesRowByTypesCars(1)}
            Me.Rows.Add(rowCarsRow)
            Return rowCarsRow
        End Function
        
        Public Function FindBylicense(ByVal license As String) As CarsRow
            Return CType(Me.Rows.Find(New Object() {license}),CarsRow)
        End Function
        
        Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return Me.Rows.GetEnumerator
        End Function
        
        Public Overrides Function Clone() As DataTable
            Dim cln As CarsDataTable = CType(MyBase.Clone,CarsDataTable)
            cln.InitVars
            Return cln
        End Function
        
        Protected Overrides Function CreateInstance() As DataTable
            Return New CarsDataTable
        End Function
        
        Friend Sub InitVars()
            Me.columnlicense = Me.Columns("license")
            Me.columntypeID = Me.Columns("typeID")
        End Sub
        
        Private Sub InitClass()
            Me.columnlicense = New DataColumn("license", GetType(System.String), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columnlicense)
            Me.columntypeID = New DataColumn("typeID", GetType(System.Int32), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columntypeID)
            Me.Constraints.Add(New UniqueConstraint("Constraint1", New DataColumn() {Me.columnlicense}, true))
            Me.columnlicense.AllowDBNull = false
            Me.columnlicense.Unique = true
        End Sub
        
        Public Function NewCarsRow() As CarsRow
            Return CType(Me.NewRow,CarsRow)
        End Function
        
        Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
            Return New CarsRow(builder)
        End Function
        
        Protected Overrides Function GetRowType() As System.Type
            Return GetType(CarsRow)
        End Function
        
        Protected Overrides Sub OnRowChanged(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanged(e)
            If (Not (Me.CarsRowChangedEvent) Is Nothing) Then
                RaiseEvent CarsRowChanged(Me, New CarsRowChangeEvent(CType(e.Row,CarsRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowChanging(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanging(e)
            If (Not (Me.CarsRowChangingEvent) Is Nothing) Then
                RaiseEvent CarsRowChanging(Me, New CarsRowChangeEvent(CType(e.Row,CarsRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleted(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleted(e)
            If (Not (Me.CarsRowDeletedEvent) Is Nothing) Then
                RaiseEvent CarsRowDeleted(Me, New CarsRowChangeEvent(CType(e.Row,CarsRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleting(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleting(e)
            If (Not (Me.CarsRowDeletingEvent) Is Nothing) Then
                RaiseEvent CarsRowDeleting(Me, New CarsRowChangeEvent(CType(e.Row,CarsRow), e.Action))
            End If
        End Sub
        
        Public Sub RemoveCarsRow(ByVal row As CarsRow)
            Me.Rows.Remove(row)
        End Sub
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class CarsRow
        Inherits DataRow
        
        Private tableCars As CarsDataTable
        
        Friend Sub New(ByVal rb As DataRowBuilder)
            MyBase.New(rb)
            Me.tableCars = CType(Me.Table,CarsDataTable)
        End Sub
        
        Public Property license As String
            Get
                Return CType(Me(Me.tableCars.licenseColumn),String)
            End Get
            Set
                Me(Me.tableCars.licenseColumn) = value
            End Set
        End Property
        
        Public Property typeID As Integer
            Get
                Try 
                    Return CType(Me(Me.tableCars.typeIDColumn),Integer)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableCars.typeIDColumn) = value
            End Set
        End Property
        
        Public Property TypesRow As TypesRow
            Get
                Return CType(Me.GetParentRow(Me.Table.ParentRelations("TypesCars")),TypesRow)
            End Get
            Set
                Me.SetParentRow(value, Me.Table.ParentRelations("TypesCars"))
            End Set
        End Property
        
        Public Function IstypeIDNull() As Boolean
            Return Me.IsNull(Me.tableCars.typeIDColumn)
        End Function
        
        Public Sub SettypeIDNull()
            Me(Me.tableCars.typeIDColumn) = System.Convert.DBNull
        End Sub
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class CarsRowChangeEvent
        Inherits EventArgs
        
        Private eventRow As CarsRow
        
        Private eventAction As DataRowAction
        
        Public Sub New(ByVal row As CarsRow, ByVal action As DataRowAction)
            MyBase.New
            Me.eventRow = row
            Me.eventAction = action
        End Sub
        
        Public ReadOnly Property Row As CarsRow
            Get
                Return Me.eventRow
            End Get
        End Property
        
        Public ReadOnly Property Action As DataRowAction
            Get
                Return Me.eventAction
            End Get
        End Property
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class ManufacturerDataTable
        Inherits DataTable
        Implements System.Collections.IEnumerable
        
        Private columnManufacturerID As DataColumn
        
        Private columnManufacturerName As DataColumn
        
        Friend Sub New()
            MyBase.New("Manufacturer")
            Me.InitClass
        End Sub
        
        Friend Sub New(ByVal table As DataTable)
            MyBase.New(table.TableName)
            If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
                Me.CaseSensitive = table.CaseSensitive
            End If
            If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
                Me.Locale = table.Locale
            End If
            If (table.Namespace <> table.DataSet.Namespace) Then
                Me.Namespace = table.Namespace
            End If
            Me.Prefix = table.Prefix
            Me.MinimumCapacity = table.MinimumCapacity
            Me.DisplayExpression = table.DisplayExpression
        End Sub
        
        <System.ComponentModel.Browsable(false)>  _
        Public ReadOnly Property Count As Integer
            Get
                Return Me.Rows.Count
            End Get
        End Property
        
        Friend ReadOnly Property ManufacturerIDColumn As DataColumn
            Get
                Return Me.columnManufacturerID
            End Get
        End Property
        
        Friend ReadOnly Property ManufacturerNameColumn As DataColumn
            Get
                Return Me.columnManufacturerName
            End Get
        End Property
        
        Public Default ReadOnly Property Item(ByVal index As Integer) As ManufacturerRow
            Get
                Return CType(Me.Rows(index),ManufacturerRow)
            End Get
        End Property
        
        Public Event ManufacturerRowChanged As ManufacturerRowChangeEventHandler
        
        Public Event ManufacturerRowChanging As ManufacturerRowChangeEventHandler
        
        Public Event ManufacturerRowDeleted As ManufacturerRowChangeEventHandler
        
        Public Event ManufacturerRowDeleting As ManufacturerRowChangeEventHandler
        
        Public Overloads Sub AddManufacturerRow(ByVal row As ManufacturerRow)
            Me.Rows.Add(row)
        End Sub
        
        Public Overloads Function AddManufacturerRow(ByVal ManufacturerName As String) As ManufacturerRow
            Dim rowManufacturerRow As ManufacturerRow = CType(Me.NewRow,ManufacturerRow)
            rowManufacturerRow.ItemArray = New Object() {Nothing, ManufacturerName}
            Me.Rows.Add(rowManufacturerRow)
            Return rowManufacturerRow
        End Function
        
        Public Function FindByManufacturerID(ByVal ManufacturerID As Integer) As ManufacturerRow
            Return CType(Me.Rows.Find(New Object() {ManufacturerID}),ManufacturerRow)
        End Function
        
        Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return Me.Rows.GetEnumerator
        End Function
        
        Public Overrides Function Clone() As DataTable
            Dim cln As ManufacturerDataTable = CType(MyBase.Clone,ManufacturerDataTable)
            cln.InitVars
            Return cln
        End Function
        
        Protected Overrides Function CreateInstance() As DataTable
            Return New ManufacturerDataTable
        End Function
        
        Friend Sub InitVars()
            Me.columnManufacturerID = Me.Columns("ManufacturerID")
            Me.columnManufacturerName = Me.Columns("ManufacturerName")
        End Sub
        
        Private Sub InitClass()
            Me.columnManufacturerID = New DataColumn("ManufacturerID", GetType(System.Int32), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columnManufacturerID)
            Me.columnManufacturerName = New DataColumn("ManufacturerName", GetType(System.String), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columnManufacturerName)
            Me.Constraints.Add(New UniqueConstraint("Constraint1", New DataColumn() {Me.columnManufacturerID}, true))
            Me.columnManufacturerID.AutoIncrement = true
            Me.columnManufacturerID.AllowDBNull = false
            Me.columnManufacturerID.Unique = true
        End Sub
        
        Public Function NewManufacturerRow() As ManufacturerRow
            Return CType(Me.NewRow,ManufacturerRow)
        End Function
        
        Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
            Return New ManufacturerRow(builder)
        End Function
        
        Protected Overrides Function GetRowType() As System.Type
            Return GetType(ManufacturerRow)
        End Function
        
        Protected Overrides Sub OnRowChanged(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanged(e)
            If (Not (Me.ManufacturerRowChangedEvent) Is Nothing) Then
                RaiseEvent ManufacturerRowChanged(Me, New ManufacturerRowChangeEvent(CType(e.Row,ManufacturerRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowChanging(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanging(e)
            If (Not (Me.ManufacturerRowChangingEvent) Is Nothing) Then
                RaiseEvent ManufacturerRowChanging(Me, New ManufacturerRowChangeEvent(CType(e.Row,ManufacturerRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleted(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleted(e)
            If (Not (Me.ManufacturerRowDeletedEvent) Is Nothing) Then
                RaiseEvent ManufacturerRowDeleted(Me, New ManufacturerRowChangeEvent(CType(e.Row,ManufacturerRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleting(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleting(e)
            If (Not (Me.ManufacturerRowDeletingEvent) Is Nothing) Then
                RaiseEvent ManufacturerRowDeleting(Me, New ManufacturerRowChangeEvent(CType(e.Row,ManufacturerRow), e.Action))
            End If
        End Sub
        
        Public Sub RemoveManufacturerRow(ByVal row As ManufacturerRow)
            Me.Rows.Remove(row)
        End Sub
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class ManufacturerRow
        Inherits DataRow
        
        Private tableManufacturer As ManufacturerDataTable
        
        Friend Sub New(ByVal rb As DataRowBuilder)
            MyBase.New(rb)
            Me.tableManufacturer = CType(Me.Table,ManufacturerDataTable)
        End Sub
        
        Public Property ManufacturerID As Integer
            Get
                Return CType(Me(Me.tableManufacturer.ManufacturerIDColumn),Integer)
            End Get
            Set
                Me(Me.tableManufacturer.ManufacturerIDColumn) = value
            End Set
        End Property
        
        Public Property ManufacturerName As String
            Get
                Try 
                    Return CType(Me(Me.tableManufacturer.ManufacturerNameColumn),String)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableManufacturer.ManufacturerNameColumn) = value
            End Set
        End Property
        
        Public Function IsManufacturerNameNull() As Boolean
            Return Me.IsNull(Me.tableManufacturer.ManufacturerNameColumn)
        End Function
        
        Public Sub SetManufacturerNameNull()
            Me(Me.tableManufacturer.ManufacturerNameColumn) = System.Convert.DBNull
        End Sub
        
        Public Function GetTypesRows() As TypesRow()
            Return CType(Me.GetChildRows(Me.Table.ChildRelations("ManufacturerTypes")),TypesRow())
        End Function
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class ManufacturerRowChangeEvent
        Inherits EventArgs
        
        Private eventRow As ManufacturerRow
        
        Private eventAction As DataRowAction
        
        Public Sub New(ByVal row As ManufacturerRow, ByVal action As DataRowAction)
            MyBase.New
            Me.eventRow = row
            Me.eventAction = action
        End Sub
        
        Public ReadOnly Property Row As ManufacturerRow
            Get
                Return Me.eventRow
            End Get
        End Property
        
        Public ReadOnly Property Action As DataRowAction
            Get
                Return Me.eventAction
            End Get
        End Property
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class TypesDataTable
        Inherits DataTable
        Implements System.Collections.IEnumerable
        
        Private columnmanufacturerID As DataColumn
        
        Private columntypeID As DataColumn
        
        Private columntypeName As DataColumn
        
        Friend Sub New()
            MyBase.New("Types")
            Me.InitClass
        End Sub
        
        Friend Sub New(ByVal table As DataTable)
            MyBase.New(table.TableName)
            If (table.CaseSensitive <> table.DataSet.CaseSensitive) Then
                Me.CaseSensitive = table.CaseSensitive
            End If
            If (table.Locale.ToString <> table.DataSet.Locale.ToString) Then
                Me.Locale = table.Locale
            End If
            If (table.Namespace <> table.DataSet.Namespace) Then
                Me.Namespace = table.Namespace
            End If
            Me.Prefix = table.Prefix
            Me.MinimumCapacity = table.MinimumCapacity
            Me.DisplayExpression = table.DisplayExpression
        End Sub
        
        <System.ComponentModel.Browsable(false)>  _
        Public ReadOnly Property Count As Integer
            Get
                Return Me.Rows.Count
            End Get
        End Property
        
        Friend ReadOnly Property manufacturerIDColumn As DataColumn
            Get
                Return Me.columnmanufacturerID
            End Get
        End Property
        
        Friend ReadOnly Property typeIDColumn As DataColumn
            Get
                Return Me.columntypeID
            End Get
        End Property
        
        Friend ReadOnly Property typeNameColumn As DataColumn
            Get
                Return Me.columntypeName
            End Get
        End Property
        
        Public Default ReadOnly Property Item(ByVal index As Integer) As TypesRow
            Get
                Return CType(Me.Rows(index),TypesRow)
            End Get
        End Property
        
        Public Event TypesRowChanged As TypesRowChangeEventHandler
        
        Public Event TypesRowChanging As TypesRowChangeEventHandler
        
        Public Event TypesRowDeleted As TypesRowChangeEventHandler
        
        Public Event TypesRowDeleting As TypesRowChangeEventHandler
        
        Public Overloads Sub AddTypesRow(ByVal row As TypesRow)
            Me.Rows.Add(row)
        End Sub
        
        Public Overloads Function AddTypesRow(ByVal parentManufacturerRowByManufacturerTypes As ManufacturerRow, ByVal typeName As String) As TypesRow
            Dim rowTypesRow As TypesRow = CType(Me.NewRow,TypesRow)
            rowTypesRow.ItemArray = New Object() {parentManufacturerRowByManufacturerTypes(0), Nothing, typeName}
            Me.Rows.Add(rowTypesRow)
            Return rowTypesRow
        End Function
        
        Public Function FindBytypeID(ByVal typeID As Integer) As TypesRow
            Return CType(Me.Rows.Find(New Object() {typeID}),TypesRow)
        End Function
        
        Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
            Return Me.Rows.GetEnumerator
        End Function
        
        Public Overrides Function Clone() As DataTable
            Dim cln As TypesDataTable = CType(MyBase.Clone,TypesDataTable)
            cln.InitVars
            Return cln
        End Function
        
        Protected Overrides Function CreateInstance() As DataTable
            Return New TypesDataTable
        End Function
        
        Friend Sub InitVars()
            Me.columnmanufacturerID = Me.Columns("manufacturerID")
            Me.columntypeID = Me.Columns("typeID")
            Me.columntypeName = Me.Columns("typeName")
        End Sub
        
        Private Sub InitClass()
            Me.columnmanufacturerID = New DataColumn("manufacturerID", GetType(System.Int32), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columnmanufacturerID)
            Me.columntypeID = New DataColumn("typeID", GetType(System.Int32), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columntypeID)
            Me.columntypeName = New DataColumn("typeName", GetType(System.String), Nothing, System.Data.MappingType.Element)
            Me.Columns.Add(Me.columntypeName)
            Me.Constraints.Add(New UniqueConstraint("Constraint1", New DataColumn() {Me.columntypeID}, true))
            Me.columntypeID.AutoIncrement = true
            Me.columntypeID.AllowDBNull = false
            Me.columntypeID.Unique = true
        End Sub
        
        Public Function NewTypesRow() As TypesRow
            Return CType(Me.NewRow,TypesRow)
        End Function
        
        Protected Overrides Function NewRowFromBuilder(ByVal builder As DataRowBuilder) As DataRow
            Return New TypesRow(builder)
        End Function
        
        Protected Overrides Function GetRowType() As System.Type
            Return GetType(TypesRow)
        End Function
        
        Protected Overrides Sub OnRowChanged(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanged(e)
            If (Not (Me.TypesRowChangedEvent) Is Nothing) Then
                RaiseEvent TypesRowChanged(Me, New TypesRowChangeEvent(CType(e.Row,TypesRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowChanging(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowChanging(e)
            If (Not (Me.TypesRowChangingEvent) Is Nothing) Then
                RaiseEvent TypesRowChanging(Me, New TypesRowChangeEvent(CType(e.Row,TypesRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleted(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleted(e)
            If (Not (Me.TypesRowDeletedEvent) Is Nothing) Then
                RaiseEvent TypesRowDeleted(Me, New TypesRowChangeEvent(CType(e.Row,TypesRow), e.Action))
            End If
        End Sub
        
        Protected Overrides Sub OnRowDeleting(ByVal e As DataRowChangeEventArgs)
            MyBase.OnRowDeleting(e)
            If (Not (Me.TypesRowDeletingEvent) Is Nothing) Then
                RaiseEvent TypesRowDeleting(Me, New TypesRowChangeEvent(CType(e.Row,TypesRow), e.Action))
            End If
        End Sub
        
        Public Sub RemoveTypesRow(ByVal row As TypesRow)
            Me.Rows.Remove(row)
        End Sub
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class TypesRow
        Inherits DataRow
        
        Private tableTypes As TypesDataTable
        
        Friend Sub New(ByVal rb As DataRowBuilder)
            MyBase.New(rb)
            Me.tableTypes = CType(Me.Table,TypesDataTable)
        End Sub
        
        Public Property manufacturerID As Integer
            Get
                Try 
                    Return CType(Me(Me.tableTypes.manufacturerIDColumn),Integer)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableTypes.manufacturerIDColumn) = value
            End Set
        End Property
        
        Public Property typeID As Integer
            Get
                Return CType(Me(Me.tableTypes.typeIDColumn),Integer)
            End Get
            Set
                Me(Me.tableTypes.typeIDColumn) = value
            End Set
        End Property
        
        Public Property typeName As String
            Get
                Try 
                    Return CType(Me(Me.tableTypes.typeNameColumn),String)
                Catch e As InvalidCastException
                    Throw New StrongTypingException("Cannot get value because it is DBNull.", e)
                End Try
            End Get
            Set
                Me(Me.tableTypes.typeNameColumn) = value
            End Set
        End Property
        
        Public Property ManufacturerRow As ManufacturerRow
            Get
                Return CType(Me.GetParentRow(Me.Table.ParentRelations("ManufacturerTypes")),ManufacturerRow)
            End Get
            Set
                Me.SetParentRow(value, Me.Table.ParentRelations("ManufacturerTypes"))
            End Set
        End Property
        
        Public Function IsmanufacturerIDNull() As Boolean
            Return Me.IsNull(Me.tableTypes.manufacturerIDColumn)
        End Function
        
        Public Sub SetmanufacturerIDNull()
            Me(Me.tableTypes.manufacturerIDColumn) = System.Convert.DBNull
        End Sub
        
        Public Function IstypeNameNull() As Boolean
            Return Me.IsNull(Me.tableTypes.typeNameColumn)
        End Function
        
        Public Sub SettypeNameNull()
            Me(Me.tableTypes.typeNameColumn) = System.Convert.DBNull
        End Sub
        
        Public Function GetCarsRows() As CarsRow()
            Return CType(Me.GetChildRows(Me.Table.ChildRelations("TypesCars")),CarsRow())
        End Function
    End Class
    
    <System.Diagnostics.DebuggerStepThrough()>  _
    Public Class TypesRowChangeEvent
        Inherits EventArgs
        
        Private eventRow As TypesRow
        
        Private eventAction As DataRowAction
        
        Public Sub New(ByVal row As TypesRow, ByVal action As DataRowAction)
            MyBase.New
            Me.eventRow = row
            Me.eventAction = action
        End Sub
        
        Public ReadOnly Property Row As TypesRow
            Get
                Return Me.eventRow
            End Get
        End Property
        
        Public ReadOnly Property Action As DataRowAction
            Get
                Return Me.eventAction
            End Get
        End Property
    End Class
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Omri started coding way back in the good ol' Apple ][ days. His first commercial (acutally governmental) software package was released 1986.
In the 90's Omri programmed C and C++, and managed larger development teams using MFC/COM.
In the last 3 years he is into Java server programming, and while he loves the language and community, he has many reservations. Maybe this is why .Net feels like comming back home...

Comments and Discussions