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(16, 8)
Me.DataGrid1.Size = New System.Drawing.Size(264, 256)
Me.DataGrid1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
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.EventArgs) Handles 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