VB.net中DataTable.Columns.Add的数据类型

来源:http://www.java2s.com/Code/VBAPI/System.Data/DataTableColumnsAdd.htm




Imports  System.Data
Imports  System.Data.OleDb
Imports  System.Windows.Forms

public class  BindDataTableToControl
    public Shared Sub  Main
         Application.Run ( New Form1 )
    End Sub
End class



Public Class  Form1
     ' T he data source.
     Private  m_ContactsTable  As  DataTable

     ' T he data source 's  CurrencyManager.
     Private  m_CurrencyManager  As  CurrencyManager

     Private Sub  Form1_Load ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles MyBase.Load
         ' M ake a DataTable.
         m_ContactsTable = New DataTable ( "Contacts" )

         ' A dd columns.
         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 ake the combined FirstName/LastName unique.
         Dim  first_last_columns ()  As  DataColumn =  _
             m_ContactsTable.Columns ( "FirstName" ) , _
             m_ContactsTable.Columns ( "LastName" _
         }
         m_ContactsTable.Constraints.Add _
             New UniqueConstraint ( first_last_columns ))

         ' M ake some contact data.
         m_ContactsTable.Rows.Add ( New Object () { "A" "B" , "C" "D" "E" "11111" })
         m_ContactsTable.Rows.Add ( New Object () { "F" "G" , "H" "I" "J" "22222" })
         m_ContactsTable.Rows.Add ( New Object () { "K" "L" , "M" "N" "O" "33333" })
         m_ContactsTable.Rows.Add ( New Object () { "P" "Q" , "R" "S" "T" "44444" })
         m_ContactsTable.Rows.Add ( New Object () { "U" "V" , "W" "X" "Y" "55555" })
         m_ContactsTable.Rows.Add ( New Object () { "Z" "A" , "B" "C" "D" "66666" })
         m_ContactsTable.Rows.Add ( New Object () { "E" "F" , "G" "H" "I" "77777" })
         m_ContactsTable.Rows.Add ( New Object () { "J" "K" , "L" "M" "N" "88888" })

         ' B ind to controls.
         txtFirstName.DataBindings.Add ( "Text" , m_ContactsTable,  "FirstName" )
         txtLastName.DataBindings.Add ( "Text" , m_ContactsTable,  "LastName" )
         txtStreet.DataBindings.Add ( "Text" , m_ContactsTable,  "Street" )
         txtCity.DataBindings.Add ( "Text" , m_ContactsTable,  "City" )
         txtState.DataBindings.Add ( "Text" , m_ContactsTable,  "State" )
         txtZip.DataBindings.Add ( "Text" , m_ContactsTable,  "Zip" )

         ' S ave a reference to the CurrencyManager.
         m_CurrencyManager = _
             DirectCast ( Me.BindingContext ( m_ContactsTable ) , CurrencyManager )
     End Sub

     Private Sub  btnFirst_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnFirst.Click
         m_CurrencyManager.Position =  0
     End Sub

     Private Sub  btnPrev_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnPrev.Click
         m_CurrencyManager.Position -=  1
     End Sub

     Private Sub  btnNext_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnNext.Click
         m_CurrencyManager.Position +=  1
     End Sub

     Private Sub  btnLast_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnLast.Click
         m_CurrencyManager.Position = m_CurrencyManager.Count -  1
     End Sub

     ' A dd a  new  record.
     Private Sub  btnAdd_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnAdd.Click
         m_CurrencyManager.AddNew ()
     End Sub

     Private Sub  btnDelete_Click ( ByVal sender  As  System.Object, _
      ByVal e  As  System.EventArgs Handles btnDelete.Click
         If  MsgBox ( "Are you sure you want to delete this record?" , _
             MsgBoxStyle.Question Or MsgBoxStyle.YesNo, _
             "Confirm Delete?" = MsgBoxResult.Yes _
         Then
             m_CurrencyManager.RemoveAt ( m_CurrencyManager.Position )
         End If
     End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated () > _
Partial  Public Class  Form1
     Inherits  System.Windows.Forms.Form

     'Fo rm overrides dispose to clean up the component list.
     <System.Diagnostics.DebuggerNonUserCode () > _
     Protected  Overloads Overrides  Sub  Dispose ( ByVal disposing  As  Boolean )
         If  disposing AndAlso components IsNot Nothing Then
             components.Dispose ()
         End If
         MyBase.Dispose ( disposing )
     End Sub

     'Re quired by the Windows Form Designer
     Private  components  As  System.ComponentModel.IContainer

     'NO TE: The following  procedure  is required by the Windows Form Designer
     'It  can be modified  using  the Windows Form Designer.  
     'Do  not modify it  using  the code editor.
     <System.Diagnostics.DebuggerStepThrough () > _
     Private Sub  InitializeComponent ()
         Me.Label6 = New System.Windows.Forms.Label
         Me.Label5 = New System.Windows.Forms.Label
         Me.Label4 = New System.Windows.Forms.Label
         Me.Label3 = New System.Windows.Forms.Label
         Me.Label2 = New System.Windows.Forms.Label
         Me.Label1 = New System.Windows.Forms.Label
         Me.btnDelete = New System.Windows.Forms.Button
         Me.btnAdd = New System.Windows.Forms.Button
         Me.btnLast = New System.Windows.Forms.Button
         Me.btnNext = New System.Windows.Forms.Button
         Me.btnPrev = New System.Windows.Forms.Button
         Me.btnFirst = New System.Windows.Forms.Button
         Me.txtZip = New System.Windows.Forms.TextBox
         Me.txtState = New System.Windows.Forms.TextBox
         Me.txtCity = New System.Windows.Forms.TextBox
         Me.txtStreet = New System.Windows.Forms.TextBox
         Me.txtLastName = New System.Windows.Forms.TextBox
         Me.txtFirstName = New System.Windows.Forms.TextBox
         Me.SuspendLayout ()
         '
         'La bel6
         '
         Me.Label6.AutoSize = True
         Me.Label6.Location = New System.Drawing.Point ( 176 104 )
         Me.Label6.Name =  "Label6"
         Me.Label6.Size = New System.Drawing.Size ( 22 13 )
         Me.Label6.TabIndex =  35
         Me.Label6.Text =  "Zip"
         '
         'La bel5
         '
         Me.Label5.AutoSize = True
         Me.Label5.Location = New System.Drawing.Point ( 8 104 )
         Me.Label5.Name =  "Label5"
         Me.Label5.Size = New System.Drawing.Size ( 32 13 )
         Me.Label5.TabIndex =  34
         Me.Label5.Text =  "State"
         '
         'La bel4
         '
         Me.Label4.AutoSize = True
         Me.Label4.Location = New System.Drawing.Point ( 8 80 )
         Me.Label4.Name =  "Label4"
         Me.Label4.Size = New System.Drawing.Size ( 24 13 )
         Me.Label4.TabIndex =  33
         Me.Label4.Text =  "City"
         '
         'La bel3
         '
         Me.Label3.AutoSize = True
         Me.Label3.Location = New System.Drawing.Point ( 8 56 )
         Me.Label3.Name =  "Label3"
         Me.Label3.Size = New System.Drawing.Size ( 35 13 )
         Me.Label3.TabIndex =  32
         Me.Label3.Text =  "Street"
         '
         'La bel2
         '
         Me.Label2.AutoSize = True
         Me.Label2.Location = New System.Drawing.Point ( 8 32 )
         Me.Label2.Name =  "Label2"
         Me.Label2.Size = New System.Drawing.Size ( 58 13 )
         Me.Label2.TabIndex =  31
         Me.Label2.Text =  "Last Name"
         '
         'La bel1
         '
         Me.Label1.AutoSize = True
         Me.Label1.Location = New System.Drawing.Point ( 8 8 )
         Me.Label1.Name =  "Label1"
         Me.Label1.Size = New System.Drawing.Size ( 57 13 )
         Me.Label1.TabIndex =  30
         Me.Label1.Text =  "First Name"
         '
         'bt nDelete
         '
         Me.btnDelete.Location = New System.Drawing.Point ( 240 144 )
         Me.btnDelete.Name =  "btnDelete"
         Me.btnDelete.Size = New System.Drawing.Size ( 32 24 )
         Me.btnDelete.TabIndex =  29
         Me.btnDelete.Text =  "X"
         '
         'bt nAdd
         '
         Me.btnAdd.Location = New System.Drawing.Point ( 200 144 )
         Me.btnAdd.Name =  "btnAdd"
         Me.btnAdd.Size = New System.Drawing.Size ( 32 24 )
         Me.btnAdd.TabIndex =  28
         Me.btnAdd.Text =  "+"
         '
         'bt nLast
         '
         Me.btnLast.Location = New System.Drawing.Point ( 104 144 )
         Me.btnLast.Name =  "btnLast"
         Me.btnLast.Size = New System.Drawing.Size ( 32 24 )
         Me.btnLast.TabIndex =  27
         Me.btnLast.Text =  ">>"
         '
         'bt nNext
         '
         Me.btnNext.Location = New System.Drawing.Point ( 72 144 )
         Me.btnNext.Name =  "btnNext"
         Me.btnNext.Size = New System.Drawing.Size ( 32 24 )
         Me.btnNext.TabIndex =  26
         Me.btnNext.Text =  ">"
         '
         'bt nPrev
         '
         Me.btnPrev.Location = New System.Drawing.Point ( 40 144 )
         Me.btnPrev.Name =  "btnPrev"
         Me.btnPrev.Size = New System.Drawing.Size ( 32 24 )
         Me.btnPrev.TabIndex =  25
         Me.btnPrev.Text =  "<"
         '
         'bt nFirst
         '
         Me.btnFirst.Location = New System.Drawing.Point ( 8 144 )
         Me.btnFirst.Name =  "btnFirst"
         Me.btnFirst.Size = New System.Drawing.Size ( 32 24 )
         Me.btnFirst.TabIndex =  24
         Me.btnFirst.Text =  "<<"
         '
         'tx tZip
         '
         Me.txtZip.Anchor = CType ((( System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left _
                     Or System.Windows.Forms.AnchorStyles.Right ) , System.Windows.Forms.AnchorStyles )
         Me.txtZip.Location = New System.Drawing.Point ( 200 104 )
         Me.txtZip.Name =  "txtZip"
         Me.txtZip.Size = New System.Drawing.Size ( 72 20 )
         Me.txtZip.TabIndex =  23
         '
         'tx tState
         '
         Me.txtState.Location = New System.Drawing.Point ( 72 104 )
         Me.txtState.Name =  "txtState"
         Me.txtState.Size = New System.Drawing.Size ( 32 20 )
         Me.txtState.TabIndex =  22
         '
         'tx tCity
         '
         Me.txtCity.Anchor = CType ((( System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left _
                     Or System.Windows.Forms.AnchorStyles.Right ) , System.Windows.Forms.AnchorStyles )
         Me.txtCity.Location = New System.Drawing.Point ( 72 80 )
         Me.txtCity.Name =  "txtCity"
         Me.txtCity.Size = New System.Drawing.Size ( 200 20 )
         Me.txtCity.TabIndex =  21
         '
         'tx tStreet
         '
         Me.txtStreet.Anchor = CType ((( System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left _
                     Or System.Windows.Forms.AnchorStyles.Right ) , System.Windows.Forms.AnchorStyles )
         Me.txtStreet.Location = New System.Drawing.Point ( 72 56 )
         Me.txtStreet.Name =  "txtStreet"
         Me.txtStreet.Size = New System.Drawing.Size ( 200 20 )
         Me.txtStreet.TabIndex =  20
         '
         'tx tLastName
         '
         Me.txtLastName.Anchor = CType ((( System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left _
                     Or System.Windows.Forms.AnchorStyles.Right ) , System.Windows.Forms.AnchorStyles )
         Me.txtLastName.Location = New System.Drawing.Point ( 72 32 )
         Me.txtLastName.Name =  "txtLastName"
         Me.txtLastName.Size = New System.Drawing.Size ( 200 20 )
         Me.txtLastName.TabIndex =  19
         '
         'tx tFirstName
         '
         Me.txtFirstName.Anchor = CType ((( System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left _
                     Or System.Windows.Forms.AnchorStyles.Right ) , System.Windows.Forms.AnchorStyles )
         Me.txtFirstName.Location = New System.Drawing.Point ( 72 8 )
         Me.txtFirstName.Name =  "txtFirstName"
         Me.txtFirstName.Size = New System.Drawing.Size ( 200 20 )
         Me.txtFirstName.TabIndex =  18
         '
         'Fo rm1
         '
         Me.AutoScaleDimensions = New System.Drawing.SizeF ( 6.0 !,  13.0 ! )
         Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
         Me.ClientSize = New System.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 ()

     End Sub
     Friend WithEvents Label6  As  System.Windows.Forms.Label
     Friend WithEvents Label5  As  System.Windows.Forms.Label
     Friend WithEvents Label4  As  System.Windows.Forms.Label
     Friend WithEvents Label3  As  System.Windows.Forms.Label
     Friend WithEvents Label2  As  System.Windows.Forms.Label
     Friend WithEvents Label1  As  System.Windows.Forms.Label
     Friend WithEvents btnDelete  As  System.Windows.Forms.Button
     Friend WithEvents btnAdd  As  System.Windows.Forms.Button
     Friend WithEvents btnLast  As  System.Windows.Forms.Button
     Friend WithEvents btnNext  As  System.Windows.Forms.Button
     Friend WithEvents btnPrev  As  System.Windows.Forms.Button
     Friend WithEvents btnFirst  As  System.Windows.Forms.Button
     Friend WithEvents txtZip  As  System.Windows.Forms.TextBox
     Friend WithEvents txtState  As  System.Windows.Forms.TextBox
     Friend WithEvents txtCity  As  System.Windows.Forms.TextBox
     Friend WithEvents txtStreet  As  System.Windows.Forms.TextBox
     Friend WithEvents txtLastName  As  System.Windows.Forms.TextBox
     Friend WithEvents txtFirstName  As  System.Windows.Forms.TextBox

End Class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值