DataColumn Collection demo : DataColumn « Database ADO.net « VB.Net

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Development
8.Event
9.File Directory
10.Generics
11.GUI
12.Language Basics
13.LINQ
14.Network Remote
15.Security
16.Thread
17.Windows Presentation Foundation
18.Windows System
19.XML
20.XML LINQ
VB.Net Tutorial
VB.Net by API
VB.Net » Database ADO.net » DataColumnScreenshots 
DataColumn Collection demo
  

Imports System.Data
Imports System.Windows.Forms

Public Class Form1
    Inherits System.Windows.Forms.Form
    Private custTable As DataTable
    Private dtSet As DataSet

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub


    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid

    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()

        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.Location = New System.Drawing.Point(168)
        Me.DataGrid1.Size = New System.Drawing.Size(264256)
        Me.DataGrid1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(292273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgsHandles MyBase.Load
        custTable = New DataTable("Customers")
        Dim dtColumn As DataColumn
        Dim myDataRow As DataRow

        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.Int32")
        dtColumn.ColumnName = "id"
        dtColumn.Caption = "Cust ID"
        dtColumn.ReadOnly = False
        dtColumn.Unique = True

        custTable.Columns.Add(dtColumn)
        ' Create Name column.
        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.String")
        dtColumn.ColumnName = "Name"
        dtColumn.Caption = "Cust Name"
        dtColumn.AutoIncrement = False
        dtColumn.ReadOnly = False
        dtColumn.Unique = False

        custTable.Columns.Add(dtColumn)

        dtColumn = New DataColumn()
        dtColumn.DataType = System.Type.GetType("System.String")
        dtColumn.ColumnName = "Address"
        dtColumn.Caption = "Address"
        dtColumn.ReadOnly = False
        dtColumn.Unique = False

        custTable.Columns.Add(dtColumn)

        Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
        PrimaryKeyColumns(0= custTable.Columns("id")
        custTable.PrimaryKey = PrimaryKeyColumns
        dtSet = New DataSet("Customers")
        dtSet.Tables.Add(custTable)

        myDataRow = custTable.NewRow()
        myDataRow("id"1001
        myDataRow("Address""CA"
        myDataRow("Name""A"
        custTable.Rows.Add(myDataRow)
        myDataRow = custTable.NewRow()
        myDataRow("id"1002
        myDataRow("Name""Jack "
        myDataRow("Address""CA"
        custTable.Rows.Add(myDataRow)
        myDataRow = custTable.NewRow()
        myDataRow("id"1003
        myDataRow("Name""M"
        myDataRow("Address""BC"
        custTable.Rows.Add(myDataRow)

        Dim cols As DataColumnCollection = custTable.Columns
        Dim str As String
        str = cols.Count.ToString()
        MessageBox.Show(str)

        If cols.Contains("Name"Then
            If cols.CanRemove(cols("Name")) Then
                cols.Remove("Name")
            End If
        End If

        DataGrid1.DataSource = dtSet.DefaultViewManager
    End Sub
End Class

   
    
  
Related examples in the same category
1.Create primary key DataColumn
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.