2008年6月14日 星期六

Demo how to use DataTable, DataGrid and …[2008_06_14_PM_03_12_58]

' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' '  [2008_06_14_PM_03_12_58]  ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
Public Class Form2
    Inherits System.Windows.Forms.Form

    ' Put the next line into the Declarations section.
    Private dataSet As DataSet

#Region " Windows Form 設計工具產生的程式碼 "

    Public Sub New()
        MyBase.New()

        '此為 Windows Form 設計工具所需的呼叫。
        InitializeComponent()

        '在 InitializeComponent() 呼叫之後加入所有的初始設定

    End Sub

    'Form 覆寫 Dispose 以清除元件清單。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    '為 Windows Form 設計工具的必要項
    Private components As System.ComponentModel.IContainer

    '注意: 以下為 Windows Form 設計工具所需的程序
    '您可以使用 Windows Form 設計工具進行修改。
    '請勿使用程式碼編輯器來修改這些程序。
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents miTest01 As System.Windows.Forms.MenuItem
    Friend WithEvents miPublishSourceCode As System.Windows.Forms.MenuItem
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.MainMenu1 = New System.Windows.Forms.MainMenu
        Me.MenuItem1 = New System.Windows.Forms.MenuItem
        Me.miTest01 = New System.Windows.Forms.MenuItem
        Me.miPublishSourceCode = New System.Windows.Forms.MenuItem
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(464, 445)
        Me.DataGrid1.TabIndex = 0
        '
        'MainMenu1
        '
        Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
        '
        'MenuItem1
        '
        Me.MenuItem1.Index = 0
        Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.miTest01, Me.miPublishSourceCode})
        Me.MenuItem1.Text = "Test"
        '
        'miTest01
        '
        Me.miTest01.Index = 0
        Me.miTest01.Text = "Test01"
        '
        'miPublishSourceCode
        '
        Me.miPublishSourceCode.Index = 1
        Me.miPublishSourceCode.Text = "Publish Source Code"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
        Me.ClientSize = New System.Drawing.Size(464, 445)
        Me.Controls.Add(Me.DataGrid1)
        Me.Menu = Me.MainMenu1
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


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

    Private Sub MakeParentTable()
        ' Create a new DataTable.
        Dim table As DataTable = New DataTable("ParentTable")

        ' Declare variables for DataColumn and DataRow objects.
        Dim column As DataColumn
        Dim row As DataRow

        ' Create new DataColumn, set DataType, ColumnName
        ' and add to DataTable.   
        column = New DataColumn
        column.DataType = System.Type.GetType("System.Int32")
        column.ColumnName = "id"
        column.ReadOnly = True
        column.Unique = True

        ' Add the Column to the DataColumnCollection.
        table.Columns.Add(column)

        ' Create second column.
        column = New DataColumn
        column.DataType = System.Type.GetType("System.String")
        column.ColumnName = "ParentItem"
        column.AutoIncrement = False
        column.Caption = "ParentItem"
        column.ReadOnly = False
        column.Unique = False

        ' Add the column to the table.
        table.Columns.Add(column)

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

        ' Instantiate the DataSet variable.
        dataSet = New DataSet

        ' Add the new DataTable to the DataSet.
        dataSet.Tables.Add(table)

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

    Private Sub MakeChildTable()
        ' Create a new DataTable.
        Dim table As DataTable = New DataTable("childTable")
        Dim column As DataColumn
        Dim row As DataRow

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

        ' Add the column to the DataColumnCollection.
        table.Columns.Add(column)

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

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

        dataSet.Tables.Add(table)

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

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

    Private Sub BindToDataGrid()
        ' Instruct the DataGrid to bind to the DataSet, with the
        ' ParentTable as the topmost DataTable.
        DataGrid1.SetDataBinding(dataSet, "ParentTable")
    End Sub

    Private Sub miTest01_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miTest01.Click
        Me.MakeDataTables()
        Me.MakeParentTable()
        Me.MakeChildTable()
        Me.MakeDataRelation()
        Me.BindToDataGrid()
    End Sub

    Private Sub miPublishSourceCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miPublishSourceCode.Click
        Dim H As New SourceCodePublishingHelper_VBY2008M06

        H.TreatClipboardContentAsVBSourceCodeAndMakeItReadyForPostingToBlogSpot()

    End Sub
End Class

' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' '  [2008_06_14_PM_03_12_58]  ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

沒有留言: