VB.Net/Database ADO.net/DataColumn
Create primary key DataColumn
Imports System.Data
Imports System.Windows.Forms
Public Class Form1
Inherits System.Windows.Forms.Form
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Public Sub New()
MyBase.New()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(0, 8)
Me.DataGrid1.Size = New System.Drawing.Size(432, 256)
Me.DataGrid1.TabIndex = 0
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateCustomersTable()
End Sub
Private Sub dcConstructorsTest()
Dim custTable As DataTable = New DataTable("Customers")
Dim dtSet As DataSet = New DataSet()
Dim myDataType As System.Type
myDataType = System.Type.GetType("System.Int32")
Dim priceCol As DataColumn = New DataColumn("Price", myDataType)
priceCol.Caption = "Price"
custTable.Columns.Add(priceCol)
Dim qtCol As DataColumn = New DataColumn()
qtCol.ColumnName = "Quantity"
qtCol.DataType = System.Type.GetType("System.Int32")
qtCol.Caption = "Quantity"
custTable.Columns.Add(qtCol)
Dim strExpr As String = "Price * Quantity"
Dim totCol As DataColumn = New DataColumn("Total", myDataType, strExpr, MappingType.Attribute)
totCol.Caption = "Total"
custTable.Columns.Add(totCol)
dtSet.Tables.Add(custTable)
DataGrid1.SetDataBinding(dtSet, "Customers")
End Sub
Private Sub CreateCustTable()
Dim custTable As DataTable = New DataTable("Customers")
Dim IdCol As DataColumn = New DataColumn()
IdCol.ColumnName = "ID"
IdCol.DataType = Type.GetType("System.Int32")
IdCol.ReadOnly = True
IdCol.AllowDBNull = False
IdCol.Unique = True
IdCol.AutoIncrement = True
IdCol.AutoIncrementSeed = 1
IdCol.AutoIncrementStep = 1
custTable.Columns.Add(IdCol)
Dim nameCol As DataColumn = New DataColumn()
nameCol.ColumnName = "Name"
nameCol.DataType = Type.GetType("System.String")
custTable.Columns.Add(nameCol)
Dim addCol As DataColumn = New DataColumn()
addCol.ColumnName = "Address"
addCol.DataType = Type.GetType("System.String")
custTable.Columns.Add(addCol)
Dim dobCol As DataColumn = New DataColumn()
dobCol.ColumnName = "DOB"
dobCol.DataType = Type.GetType("System.DateTime")
custTable.Columns.Add(dobCol)
Dim fullTimeCol As DataColumn = New DataColumn()
fullTimeCol.ColumnName = "VAR"
fullTimeCol.DataType = Type.GetType("System.Boolean")
custTable.Columns.Add(fullTimeCol)
Dim PrimaryKeyColumns() As DataColumn = New DataColumn(1) {}
PrimaryKeyColumns(0) = custTable.Columns("ID")
custTable.PrimaryKey = PrimaryKeyColumns
Dim ds As DataSet = New DataSet("Customers")
ds.Tables.Add(custTable)
dataGrid1.DataSource = ds.DefaultViewManager
End Sub
Private Sub CreateCustomersTable()
Dim custTable As System.Data.DataTable = New DataTable("Customers")
Dim dtColumn As DataColumn
dtColumn = New DataColumn()
dtColumn.DataType = System.Type.GetType("System.Int32")
dtColumn.ColumnName = "id"
dtColumn.Caption = "Cust ID"
dtColumn.ReadOnly = True
dtColumn.Unique = True
custTable.Columns.Add(dtColumn)
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
Dim ds As DataSet = New DataSet("Customers")
ds.Tables.Add(custTable)
Dim row1 As DataRow = custTable.NewRow()
row1("id") = 1001
row1("Address") = "CA"
row1("Name") = "A"
custTable.Rows.Add(row1)
Dim row2 As DataRow = custTable.NewRow()
row2("id") = 1002
row2("Name") = "R"
row2("Address") = "CA"
custTable.Rows.Add(row2)
Dim row3 As DataRow = custTable.NewRow()
row3("id") = 1003
row3("Name") = "M "
row3("Address") = "CA"
custTable.Rows.Add(row3)
MessageBox.Show(row2.RowState.ToString())
row2.Delete()
MessageBox.Show(row3.RowState.ToString())
DataGrid1.DataSource = ds.DefaultViewManager
End Sub
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
End Class
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.ruponentModel.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.ruponentModel.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