来源:http://www.java2s.com/Code/VBAPI/System.Data/DataTableColumnsAdd.htm
Imports System.Data.OleDb
Imports System.Windows.Forms
publicclass BindDataTableToControl
publicSharedSub Main
Application.Run ( NewForm1 )
EndSub
Endclass
PublicClass Form1
'T hedatasource.
Private m_ContactsTable As DataTable
'T hedatasource 's CurrencyManager.
Private m_CurrencyManager As CurrencyManager
PrivateSub Form1_Load ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesMyBase.Load
'M akeaDataTable.
m_ContactsTable=NewDataTable ( "Contacts" )
'A ddcolumns.
m_ContactsTable.Columns.Add ( "FirstName" ,GetType ( String ))
m_ContactsTable.Columns.Add ( "LastName" ,GetType ( String ))
m_ContactsTable.Columns.Add ( "Street" ,GetType ( String ))
m_ContactsTable.Columns.Add ( "City" ,GetType ( String ))
m_ContactsTable.Columns.Add ( "State" ,GetType ( String ))
m_ContactsTable.Columns.Add ( "Zip" ,GetType ( String ))
'M akethecombinedFirstName/LastNameunique.
Dim first_last_columns () As DataColumn= { _
m_ContactsTable.Columns ( "FirstName" ) ,_
m_ContactsTable.Columns ( "LastName" ) _
}
m_ContactsTable.Constraints.Add ( _
NewUniqueConstraint ( first_last_columns ))
'M akesomecontactdata.
m_ContactsTable.Rows.Add ( NewObject (){ "A" , "B" , "C" , "D" , "E" , "11111" })
m_ContactsTable.Rows.Add ( NewObject (){ "F" , "G" , "H" , "I" , "J" , "22222" })
m_ContactsTable.Rows.Add ( NewObject (){ "K" , "L" , "M" , "N" , "O" , "33333" })
m_ContactsTable.Rows.Add ( NewObject (){ "P" , "Q" , "R" , "S" , "T" , "44444" })
m_ContactsTable.Rows.Add ( NewObject (){ "U" , "V" , "W" , "X" , "Y" , "55555" })
m_ContactsTable.Rows.Add ( NewObject (){ "Z" , "A" , "66666" })
m_ContactsTable.Rows.Add ( NewObject (){ "E" , "F" , "77777" })
m_ContactsTable.Rows.Add ( NewObject (){ "J" , "K" , "88888" })
'B indtocontrols.
txtFirstName.DataBindings.Add ( "Text" ,m_ContactsTable, "FirstName" )
txtLastName.DataBindings.Add ( "Text" , "LastName" )
txtStreet.DataBindings.Add ( "Text" , "Street" )
txtCity.DataBindings.Add ( "Text" , "City" )
txtState.DataBindings.Add ( "Text" , "State" )
txtZip.DataBindings.Add ( "Text" , "Zip" )
'S aveareferencetotheCurrencyManager.
m_CurrencyManager=_
DirectCast ( Me.BindingContext ( m_ContactsTable ) ,CurrencyManager )
EndSub
PrivateSub btnFirst_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnFirst.Click
m_CurrencyManager.Position= 0
EndSub
PrivateSub btnPrev_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnPrev.Click
m_CurrencyManager.Position-= 1
EndSub
PrivateSub btnNext_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnNext.Click
m_CurrencyManager.Position+= 1
EndSub
PrivateSub btnLast_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnLast.Click
m_CurrencyManager.Position=m_CurrencyManager.Count- 1
EndSub
'A dda new record.
PrivateSub btnAdd_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnAdd.Click
m_CurrencyManager.AddNew ()
EndSub
PrivateSub btnDelete_Click ( ByValsender As System.Object,_
ByVale As System.EventArgs ) HandlesbtnDelete.Click
If MsgBox ( "Areyousureyouwanttodeletethisrecord?" ,_
MsgBoxStyle.QuestionOrMsgBoxStyle.YesNo,_
"ConfirmDelete?" ) =MsgBoxResult.Yes_
Then
m_CurrencyManager.RemoveAt ( m_CurrencyManager.Position )
EndIf
EndSub
EndClass
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated () >_
Partial PublicClass Form1
Inherits System.Windows.Forms.Form
'Fo rmoverridesdisposetocleanupthecomponentlist.
<System.Diagnostics.DebuggerNonUserCode () >_
Protected OverloadsOverrides Sub Dispose ( ByValdisposing As Boolean )
If disposingAndAlsocomponentsIsNotNothingThen
components.Dispose ()
EndIf
MyBase.Dispose ( disposing )
EndSub
'Re quiredbytheWindowsFormDesigner
Private components As System.ComponentModel.IContainer
'NO TE:Thefollowing procedure isrequiredbytheWindowsFormDesigner
'It canbemodified using theWindowsFormDesigner.
'Do notmodifyit using thecodeeditor.
<System.Diagnostics.DebuggerStepThrough () >_
PrivateSub InitializeComponent ()
Me.Label6=NewSystem.Windows.Forms.Label
Me.Label5=NewSystem.Windows.Forms.Label
Me.Label4=NewSystem.Windows.Forms.Label
Me.Label3=NewSystem.Windows.Forms.Label
Me.Label2=NewSystem.Windows.Forms.Label
Me.Label1=NewSystem.Windows.Forms.Label
Me.btnDelete=NewSystem.Windows.Forms.Button
Me.btnAdd=NewSystem.Windows.Forms.Button
Me.btnLast=NewSystem.Windows.Forms.Button
Me.btnNext=NewSystem.Windows.Forms.Button
Me.btnPrev=NewSystem.Windows.Forms.Button
Me.btnFirst=NewSystem.Windows.Forms.Button
Me.txtZip=NewSystem.Windows.Forms.TextBox
Me.txtState=NewSystem.Windows.Forms.TextBox
Me.txtCity=NewSystem.Windows.Forms.TextBox
Me.txtStreet=NewSystem.Windows.Forms.TextBox
Me.txtLastName=NewSystem.Windows.Forms.TextBox
Me.txtFirstName=NewSystem.Windows.Forms.TextBox
Me.SuspendLayout ()
'
'La bel6
'
Me.Label6.AutoSize=True
Me.Label6.Location=NewSystem.Drawing.Point ( 176 , 104 )
Me.Label6.Name= "Label6"
Me.Label6.Size=NewSystem.Drawing.Size ( 22 , 13 )
Me.Label6.TabIndex= 35
Me.Label6.Text= "Zip"
'
'La bel5
'
Me.Label5.AutoSize=True
Me.Label5.Location=NewSystem.Drawing.Point ( 8 , 104 )
Me.Label5.Name= "Label5"
Me.Label5.Size=NewSystem.Drawing.Size ( 32 , 13 )
Me.Label5.TabIndex= 34
Me.Label5.Text= "State"
'
'La bel4
'
Me.Label4.AutoSize=True
Me.Label4.Location=NewSystem.Drawing.Point ( 8 , 80 )
Me.Label4.Name= "Label4"
Me.Label4.Size=NewSystem.Drawing.Size ( 24 , 13 )
Me.Label4.TabIndex= 33
Me.Label4.Text= "City"
'
'La bel3
'
Me.Label3.AutoSize=True
Me.Label3.Location=NewSystem.Drawing.Point ( 8 , 56 )
Me.Label3.Name= "Label3"
Me.Label3.Size=NewSystem.Drawing.Size ( 35 , 13 )
Me.Label3.TabIndex= 32
Me.Label3.Text= "Street"
'
'La bel2
'
Me.Label2.AutoSize=True
Me.Label2.Location=NewSystem.Drawing.Point ( 8 , 32 )
Me.Label2.Name= "Label2"
Me.Label2.Size=NewSystem.Drawing.Size ( 58 , 13 )
Me.Label2.TabIndex= 31
Me.Label2.Text= "LastName"
'
'La bel1
'
Me.Label1.AutoSize=True
Me.Label1.Location=NewSystem.Drawing.Point ( 8 , 8 )
Me.Label1.Name= "Label1"
Me.Label1.Size=NewSystem.Drawing.Size ( 57 , 13 )
Me.Label1.TabIndex= 30
Me.Label1.Text= "FirstName"
'
'bt nDelete
'
Me.btnDelete.Location=NewSystem.Drawing.Point ( 240 , 144 )
Me.btnDelete.Name= "btnDelete"
Me.btnDelete.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnDelete.TabIndex= 29
Me.btnDelete.Text= "X"
'
'bt nAdd
'
Me.btnAdd.Location=NewSystem.Drawing.Point ( 200 , 144 )
Me.btnAdd.Name= "btnAdd"
Me.btnAdd.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnAdd.TabIndex= 28
Me.btnAdd.Text= "+"
'
'bt nLast
'
Me.btnLast.Location=NewSystem.Drawing.Point ( 104 , 144 )
Me.btnLast.Name= "btnLast"
Me.btnLast.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnLast.TabIndex= 27
Me.btnLast.Text= ">>"
'
'bt nNext
'
Me.btnNext.Location=NewSystem.Drawing.Point ( 72 , 144 )
Me.btnNext.Name= "btnNext"
Me.btnNext.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnNext.TabIndex= 26
Me.btnNext.Text= ">"
'
'bt nPrev
'
Me.btnPrev.Location=NewSystem.Drawing.Point ( 40 , 144 )
Me.btnPrev.Name= "btnPrev"
Me.btnPrev.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnPrev.TabIndex= 25
Me.btnPrev.Text= "<"
'
'bt nFirst
'
Me.btnFirst.Location=NewSystem.Drawing.Point ( 8 , 144 )
Me.btnFirst.Name= "btnFirst"
Me.btnFirst.Size=NewSystem.Drawing.Size ( 32 , 24 )
Me.btnFirst.TabIndex= 24
Me.btnFirst.Text= "<<"
'
'tx tZip
'
Me.txtZip.Anchor=CType ((( System.Windows.Forms.AnchorStyles.TopOrSystem.Windows.Forms.AnchorStyles.Left ) _
OrSystem.Windows.Forms.AnchorStyles.Right ) ,System.Windows.Forms.AnchorStyles )
Me.txtZip.Location=NewSystem.Drawing.Point ( 200 , 104 )
Me.txtZip.Name= "txtZip"
Me.txtZip.Size=NewSystem.Drawing.Size ( 72 , 20 )
Me.txtZip.TabIndex= 23
'
'tx tState
'
Me.txtState.Location=NewSystem.Drawing.Point ( 72 , 104 )
Me.txtState.Name= "txtState"
Me.txtState.Size=NewSystem.Drawing.Size ( 32 , 20 )
Me.txtState.TabIndex= 22
'
'tx tCity
'
Me.txtCity.Anchor=CType ((( System.Windows.Forms.AnchorStyles.TopOrSystem.Windows.Forms.AnchorStyles.Left ) _
OrSystem.Windows.Forms.AnchorStyles.Right ) ,System.Windows.Forms.AnchorStyles )
Me.txtCity.Location=NewSystem.Drawing.Point ( 72 , 80 )
Me.txtCity.Name= "txtCity"
Me.txtCity.Size=NewSystem.Drawing.Size ( 200 , 20 )
Me.txtCity.TabIndex= 21
'
'tx tStreet
'
Me.txtStreet.Anchor=CType ((( System.Windows.Forms.AnchorStyles.TopOrSystem.Windows.Forms.AnchorStyles.Left ) _
OrSystem.Windows.Forms.AnchorStyles.Right ) ,System.Windows.Forms.AnchorStyles )
Me.txtStreet.Location=NewSystem.Drawing.Point ( 72 , 56 )
Me.txtStreet.Name= "txtStreet"
Me.txtStreet.Size=NewSystem.Drawing.Size ( 200 , 20 )
Me.txtStreet.TabIndex= 20
'
'tx tLastName
'
Me.txtLastName.Anchor=CType ((( System.Windows.Forms.AnchorStyles.TopOrSystem.Windows.Forms.AnchorStyles.Left ) _
OrSystem.Windows.Forms.AnchorStyles.Right ) ,System.Windows.Forms.AnchorStyles )
Me.txtLastName.Location=NewSystem.Drawing.Point ( 72 , 32 )
Me.txtLastName.Name= "txtLastName"
Me.txtLastName.Size=NewSystem.Drawing.Size ( 200 , 20 )
Me.txtLastName.TabIndex= 19
'
'tx tFirstName
'
Me.txtFirstName.Anchor=CType ((( System.Windows.Forms.AnchorStyles.TopOrSystem.Windows.Forms.AnchorStyles.Left ) _
OrSystem.Windows.Forms.AnchorStyles.Right ) ,System.Windows.Forms.AnchorStyles )
Me.txtFirstName.Location=NewSystem.Drawing.Point ( 72 , 8 )
Me.txtFirstName.Name= "txtFirstName"
Me.txtFirstName.Size=NewSystem.Drawing.Size ( 200 , 20 )
Me.txtFirstName.TabIndex= 18
'
'Fo rm1
'
Me.AutoScaleDimensions=NewSystem.Drawing.SizeF ( 6.0 !, 13.0 ! )
Me.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize=NewSystem.Drawing.Size ( 278 , 175 )
Me.Controls.Add ( Me.Label6 )
Me.Controls.Add ( Me.Label5 )
Me.Controls.Add ( Me.Label4 )
Me.Controls.Add ( Me.Label3 )
Me.Controls.Add ( Me.Label2 )
Me.Controls.Add ( Me.Label1 )
Me.Controls.Add ( Me.btnDelete )
Me.Controls.Add ( Me.btnAdd )
Me.Controls.Add ( Me.btnLast )
Me.Controls.Add ( Me.btnNext )
Me.Controls.Add ( Me.btnPrev )
Me.Controls.Add ( Me.btnFirst )
Me.Controls.Add ( Me.txtZip )
Me.Controls.Add ( Me.txtState )
Me.Controls.Add ( Me.txtCity )
Me.Controls.Add ( Me.txtStreet )
Me.Controls.Add ( Me.txtLastName )
Me.Controls.Add ( Me.txtFirstName )
Me.Name= "Form1"
Me.Text= "UseCurrencyManager"
Me.ResumeLayout ( False )
Me.PerformLayout ()
EndSub
FriendWithEventsLabel6 As System.Windows.Forms.Label
FriendWithEventsLabel5 As System.Windows.Forms.Label
FriendWithEventsLabel4 As System.Windows.Forms.Label
FriendWithEventsLabel3 As System.Windows.Forms.Label
FriendWithEventsLabel2 As System.Windows.Forms.Label
FriendWithEventsLabel1 As System.Windows.Forms.Label
FriendWithEventsbtnDelete As System.Windows.Forms.Button
FriendWithEventsbtnAdd As System.Windows.Forms.Button
FriendWithEventsbtnLast As System.Windows.Forms.Button
FriendWithEventsbtnNext As System.Windows.Forms.Button
FriendWithEventsbtnPrev As System.Windows.Forms.Button
FriendWithEventsbtnFirst As System.Windows.Forms.Button
FriendWithEventstxtZip As System.Windows.Forms.TextBox
FriendWithEventstxtState As System.Windows.Forms.TextBox
FriendWithEventstxtCity As System.Windows.Forms.TextBox
FriendWithEventstxtStreet As System.Windows.Forms.TextBox
FriendWithEventstxtLastName As System.Windows.Forms.TextBox
FriendWithEventstxtFirstName As System.Windows.Forms.TextBox
EndClass