VB.Net Tutorial/Database ADO.net/DataGrid
Версия от 16:40, 26 мая 2010; (обсуждение)
Содержание
Add DataRow to DataGrid
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
public class DataBinding
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ruponentModel.IContainer
Friend WithEvents DataSet1 As System.Data.DataSet
Friend WithEvents DataTable1 As System.Data.DataTable
Friend WithEvents DataColumn1 As System.Data.DataColumn
Friend WithEvents DataColumn2 As System.Data.DataColumn
Friend WithEvents DataColumn3 As System.Data.DataColumn
Friend WithEvents DataColumn4 As System.Data.DataColumn
Friend WithEvents DataColumn5 As System.Data.DataColumn
Friend WithEvents DataColumn6 As System.Data.DataColumn
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents TextBox6 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataSet1 = New System.Data.DataSet
Me.DataTable1 = New System.Data.DataTable
Me.DataColumn1 = New System.Data.DataColumn
Me.DataColumn2 = New System.Data.DataColumn
Me.DataColumn3 = New System.Data.DataColumn
Me.DataColumn4 = New System.Data.DataColumn
Me.DataColumn5 = New System.Data.DataColumn
Me.DataColumn6 = New System.Data.DataColumn
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label2 = New System.Windows.Forms.Label
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.TextBox3 = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.TextBox4 = New System.Windows.Forms.TextBox
Me.Label5 = New System.Windows.Forms.Label
Me.TextBox5 = New System.Windows.Forms.TextBox
Me.Label6 = New System.Windows.Forms.Label
Me.TextBox6 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.DataTable1, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"DataSet1
"
Me.DataSet1.DataSetName = "NewDataSet"
Me.DataSet1.Locale = New System.Globalization.CultureInfo("zh-CN")
Me.DataSet1.Tables.AddRange(New System.Data.DataTable() {Me.DataTable1})
"
"DataTable1
"
Me.DataTable1.Columns.AddRange(New System.Data.DataColumn() {Me.DataColumn1, Me.DataColumn2, Me.DataColumn3, Me.DataColumn4, Me.DataColumn5, Me.DataColumn6})
Me.DataTable1.TableName = "Table1"
"
"DataColumn1
"
Me.DataColumn1.Caption = "A"
Me.DataColumn1.ColumnName = "A"
"
"DataColumn2
"
Me.DataColumn2.ColumnName = "B"
Me.DataColumn2.DataType = GetType(System.Int32)
"
"DataColumn3
"
Me.DataColumn3.Caption = "C"
Me.DataColumn3.ColumnName = "C"
Me.DataColumn3.DataType = GetType(System.Int32)
"
"DataColumn4
"
Me.DataColumn4.ColumnName = "D"
Me.DataColumn4.DataType = GetType(System.Int32)
"
"DataColumn5
"
Me.DataColumn5.ColumnName = "E"
Me.DataColumn5.DataType = GetType(System.Int32)
"
"DataColumn6
"
Me.DataColumn6.ColumnName = "F"
"
"DataGrid1
"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.DataSource = Me.DataTable1
Me.DataGrid1.Dock = System.Windows.Forms.DockStyle.Top
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(488, 216)
Me.DataGrid1.TabIndex = 0
"
"Label1
"
Me.Label1.Location = New System.Drawing.Point(24, 224)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 24)
Me.Label1.TabIndex = 1
Me.Label1.Text = "A"
"
"TextBox1
"
Me.TextBox1.Location = New System.Drawing.Point(64, 224)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(64, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
"
"Label2
"
Me.Label2.Location = New System.Drawing.Point(168, 224)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 24)
Me.Label2.TabIndex = 3
Me.Label2.Text = "B"
"
"TextBox2
"
Me.TextBox2.Location = New System.Drawing.Point(216, 224)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(72, 20)
Me.TextBox2.TabIndex = 4
Me.TextBox2.Text = ""
"
"Label3
"
Me.Label3.Location = New System.Drawing.Point(24, 256)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(48, 24)
Me.Label3.TabIndex = 5
Me.Label3.Text = "C"
"
"TextBox3
"
Me.TextBox3.Location = New System.Drawing.Point(64, 256)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(64, 20)
Me.TextBox3.TabIndex = 6
Me.TextBox3.Text = ""
"
"Label4
"
Me.Label4.Location = New System.Drawing.Point(168, 256)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(48, 24)
Me.Label4.TabIndex = 7
Me.Label4.Text = "D"
"
"TextBox4
"
Me.TextBox4.Location = New System.Drawing.Point(216, 256)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(72, 20)
Me.TextBox4.TabIndex = 8
Me.TextBox4.Text = ""
"
"Label5
"
Me.Label5.Location = New System.Drawing.Point(24, 288)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(48, 24)
Me.Label5.TabIndex = 9
Me.Label5.Text = "E"
"
"TextBox5
"
Me.TextBox5.Location = New System.Drawing.Point(64, 288)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(64, 20)
Me.TextBox5.TabIndex = 10
Me.TextBox5.Text = ""
"
"Label6
"
Me.Label6.Location = New System.Drawing.Point(144, 288)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(80, 24)
Me.Label6.TabIndex = 11
Me.Label6.Text = "F"
"
"TextBox6
"
Me.TextBox6.Location = New System.Drawing.Point(216, 288)
Me.TextBox6.Name = "TextBox6"
Me.TextBox6.Size = New System.Drawing.Size(72, 20)
Me.TextBox6.TabIndex = 12
Me.TextBox6.Text = ""
"
"Button1
"
Me.Button1.Location = New System.Drawing.Point(328, 224)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 13
Me.Button1.Text = "Display"
"
"Button2
"
Me.Button2.Location = New System.Drawing.Point(328, 264)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 24)
Me.Button2.TabIndex = 14
Me.Button2.Text = "Add"
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(488, 318)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox6)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.DataGrid1)
CType(Me.DataSet1, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.DataTable1, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim row1 As DataRow
row1 = DataTable1.NewRow
row1("A") = "Tom"
row1("B") = 121
row1("C") = 145
row1("D") = 134
row1("E") = 127
row1("F") = "032158"
DataTable1.Rows.Add(row1)
row1 = DataTable1.NewRow
row1("A") = "John"
row1("B") = 95
row1("C") = 102
row1("D") = 94
row1("E") = 85
row1("F") = "032176"
DataTable1.Rows.Add(row1)
row1 = DataTable1.NewRow
row1("A") = "Alice"
row1("B") = 137
row1("C") = 96
row1("D") = 125
row1("E") = 94
row1("F") = "032152"
DataTable1.Rows.Add(row1)
row1 = DataTable1.NewRow
row1("A") = "Jack"
row1("B") = 98
row1("C") = 134
row1("D") = 87
row1("E") = 124
row1("F") = "032156"
DataTable1.Rows.Add(row1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim row1 As DataRow
row1 = DataTable1.NewRow
row1("A") = TextBox1.Text
row1("B") = 123
row1("C") = 123
row1("D") = 123
row1("E") = 123
row1("F") = TextBox6.Text
DataTable1.Rows.Add(row1)
DataTable1.AcceptChanges()
End Sub
End Class
Create DataTable and add to DataGrid
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OleDb
public class CreateDataTableAndAddToDataGrid
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim contacts_table As New DataTable("Contacts")
" Add columns.
contacts_table.Columns.Add("FirstName", GetType(String))
contacts_table.Columns.Add("LastName", GetType(String))
contacts_table.Columns.Add("Street", GetType(String))
contacts_table.Columns.Add("City", GetType(String))
contacts_table.Columns.Add("State", GetType(String))
contacts_table.Columns.Add("Zip", GetType(String))
" Make some contact data.
contacts_table.Rows.Add(New Object() {"A", "A", _
"1234", "B", "A", "11111"})
contacts_table.Rows.Add(New Object() {"B", "B", _
"22", "B", "C", "22222"})
contacts_table.Rows.Add(New Object() {"C", "C", _
"3", "P", "K", "33333"})
contacts_table.Rows.Add(New Object() {"", "D", _
"4", "P", "KS", "44444"})
grdAll.DataSource = contacts_table
grdAll.CaptionText = "All Records"
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form 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
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.SplitContainer1 = New System.Windows.Forms.SplitContainer
Me.SplitContainer2 = New System.Windows.Forms.SplitContainer
Me.grdAll = New System.Windows.Forms.DataGrid
Me.grdCO = New System.Windows.Forms.DataGrid
Me.grdName = New System.Windows.Forms.DataGrid
Me.SplitContainer1.Panel1.SuspendLayout()
Me.SplitContainer1.Panel2.SuspendLayout()
Me.SplitContainer1.SuspendLayout()
Me.SplitContainer2.Panel1.SuspendLayout()
Me.SplitContainer2.Panel2.SuspendLayout()
Me.SplitContainer2.SuspendLayout()
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"SplitContainer1
"
Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer1.Name = "SplitContainer1"
Me.SplitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer1.Panel1
"
Me.SplitContainer1.Panel1.Controls.Add(Me.grdAll)
"
"SplitContainer1.Panel2
"
Me.SplitContainer1.Panel2.Controls.Add(Me.SplitContainer2)
Me.SplitContainer1.Size = New System.Drawing.Size(519, 485)
Me.SplitContainer1.SplitterDistance = 180
Me.SplitContainer1.TabIndex = 2
Me.SplitContainer1.Text = "SplitContainer1"
"
"SplitContainer2
"
Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer2.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer2.Name = "SplitContainer2"
Me.SplitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer2.Panel1
"
Me.SplitContainer2.Panel1.Controls.Add(Me.grdCO)
"
"SplitContainer2.Panel2
"
Me.SplitContainer2.Panel2.Controls.Add(Me.grdName)
Me.SplitContainer2.Size = New System.Drawing.Size(519, 301)
Me.SplitContainer2.SplitterDistance = 173
Me.SplitContainer2.TabIndex = 0
Me.SplitContainer2.Text = "SplitContainer2"
"
"grdAll
"
Me.grdAll.DataMember = ""
Me.grdAll.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdAll.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdAll.Location = New System.Drawing.Point(0, 0)
Me.grdAll.Name = "grdAll"
Me.grdAll.Size = New System.Drawing.Size(519, 180)
Me.grdAll.TabIndex = 0
"
"grdCO
"
Me.grdCO.DataMember = ""
Me.grdCO.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdCO.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdCO.Location = New System.Drawing.Point(0, 0)
Me.grdCO.Name = "grdCO"
Me.grdCO.Size = New System.Drawing.Size(519, 173)
Me.grdCO.TabIndex = 1
"
"grdName
"
Me.grdName.DataMember = ""
Me.grdName.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdName.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdName.Location = New System.Drawing.Point(0, 0)
Me.grdName.Name = "grdName"
Me.grdName.Size = New System.Drawing.Size(519, 124)
Me.grdName.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(519, 485)
Me.Controls.Add(Me.SplitContainer1)
Me.Name = "Form1"
Me.Text = "Contacts"
Me.SplitContainer1.Panel1.ResumeLayout(False)
Me.SplitContainer1.Panel2.ResumeLayout(False)
Me.SplitContainer1.ResumeLayout(False)
Me.SplitContainer2.Panel1.ResumeLayout(False)
Me.SplitContainer2.Panel2.ResumeLayout(False)
Me.SplitContainer2.ResumeLayout(False)
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
Friend WithEvents grdAll As System.Windows.Forms.DataGrid
Friend WithEvents SplitContainer2 As System.Windows.Forms.SplitContainer
Friend WithEvents grdCO As System.Windows.Forms.DataGrid
Friend WithEvents grdName As System.Windows.Forms.DataGrid
End Class
DataGrid: DataSet Relation
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
public class DataSetWithRelation
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim scores_dataset As New DataSet("Scores")
Dim students_table As DataTable = scores_dataset.Tables.Add("Students")
students_table.Columns.Add("FirstName", GetType(String))
students_table.Columns.Add("LastName", GetType(String))
students_table.Columns.Add("StudentId", GetType(Integer))
students_table.Columns("StudentId").Unique = True
Dim first_last_columns() As DataColumn = { _
students_table.Columns("FirstName"), _
students_table.Columns("LastName") _
}
students_table.Constraints.Add(New UniqueConstraint(first_last_columns))
Dim test_scores_table As DataTable = scores_dataset.Tables.Add("TestScores")
test_scores_table.Columns.Add("StudentId", GetType(Integer))
test_scores_table.Columns.Add("TestNumber", GetType(Integer))
test_scores_table.Columns.Add("Score", GetType(Integer))
Dim studentid_testnumber_score_columns() As DataColumn = { _
test_scores_table.Columns("StudentId"),test_scores_table.Columns("TestNumber") _
}
test_scores_table.Constraints.Add(New UniqueConstraint(studentid_testnumber_score_columns))
scores_dataset.Relations.Add("Student Test Scores",students_table.Columns("StudentId"),test_scores_table.Columns("StudentId"))
students_table.Rows.Add(New Object() {"A", "A", 1})
students_table.Rows.Add(New Object() {"B", "B", 2})
students_table.Rows.Add(New Object() {"C", "C", 3})
students_table.Rows.Add(New Object() {"D", "D", 4})
Dim score As New Random
For id As Integer = 1 To 4
For test_num As Integer = 1 To 10
test_scores_table.Rows.Add( _
New Object() {id, test_num, score.Next(65, 100)})
Next test_num
Next id
students_table.Rows(1).SetColumnError(2, "Bad name format")
students_table.Rows(2).RowError = "Missing registration"
grdScores.DataSource = scores_dataset
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form 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
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.grdScores = New System.Windows.Forms.DataGrid
CType(Me.grdScores, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"grdScores
"
Me.grdScores.DataMember = ""
Me.grdScores.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdScores.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdScores.Location = New System.Drawing.Point(0, 0)
Me.grdScores.Name = "grdScores"
Me.grdScores.Size = New System.Drawing.Size(292, 273)
Me.grdScores.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.grdScores)
Me.Name = "Form1"
Me.Text = "MemoryDataSetWithError"
CType(Me.grdScores, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents grdScores As System.Windows.Forms.DataGrid
End Class
DataGrid with Error mark
Imports System.Data
Imports System.Data.OleDb
Imports System.Windows.Forms
public class DataGridWithError
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim scores_dataset As New DataSet("Scores")
Dim students_table As DataTable = scores_dataset.Tables.Add("Students")
students_table.Columns.Add("FirstName", GetType(String))
students_table.Columns.Add("LastName", GetType(String))
students_table.Columns.Add("StudentId", GetType(Integer))
students_table.Columns("StudentId").Unique = True
Dim first_last_columns() As DataColumn = { _
students_table.Columns("FirstName"), _
students_table.Columns("LastName") _
}
students_table.Constraints.Add(New UniqueConstraint(first_last_columns))
Dim test_scores_table As DataTable = scores_dataset.Tables.Add("TestScores")
test_scores_table.Columns.Add("StudentId", GetType(Integer))
test_scores_table.Columns.Add("TestNumber", GetType(Integer))
test_scores_table.Columns.Add("Score", GetType(Integer))
Dim studentid_testnumber_score_columns() As DataColumn = { _
test_scores_table.Columns("StudentId"),test_scores_table.Columns("TestNumber") _
}
test_scores_table.Constraints.Add(New UniqueConstraint(studentid_testnumber_score_columns))
scores_dataset.Relations.Add("Student Test Scores",students_table.Columns("StudentId"),test_scores_table.Columns("StudentId"))
students_table.Rows.Add(New Object() {"A", "A", 1})
students_table.Rows.Add(New Object() {"B", "B", 2})
students_table.Rows.Add(New Object() {"C", "C", 3})
students_table.Rows.Add(New Object() {"D", "D", 4})
Dim score As New Random
For id As Integer = 1 To 4
For test_num As Integer = 1 To 10
test_scores_table.Rows.Add( _
New Object() {id, test_num, score.Next(65, 100)})
Next test_num
Next id
students_table.Rows(1).SetColumnError(2, "Bad name format")
students_table.Rows(2).RowError = "Missing registration"
grdScores.DataSource = scores_dataset
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form 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
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.grdScores = New System.Windows.Forms.DataGrid
CType(Me.grdScores, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"grdScores
"
Me.grdScores.DataMember = ""
Me.grdScores.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdScores.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdScores.Location = New System.Drawing.Point(0, 0)
Me.grdScores.Name = "grdScores"
Me.grdScores.Size = New System.Drawing.Size(292, 273)
Me.grdScores.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.grdScores)
Me.Name = "Form1"
Me.Text = "MemoryDataSetWithError"
CType(Me.grdScores, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents grdScores As System.Windows.Forms.DataGrid
End Class
Load XML data to DataGrid
Imports System.Data.SqlClient
Imports System.Data
Imports System.Windows.Forms
public class XMLDataGrid
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
"This call is required by the Windows Form Designer.
InitializeComponent()
"Add any initialization after the InitializeComponent() call
End Sub
"Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.
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()
"
"DataGrid1
"
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(16, 32)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(664, 160)
Me.DataGrid1.TabIndex = 0
"
"Form1
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(736, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1})
Me.Name = "Form1"
Me.Text = "DataGrid Demo"
CType(Me.DataGrid1, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DS As New DataSet()
DS.ReadXml("Authors.XML")
DataGrid1.CaptionText = "Book Information"
DataGrid1.SetDataBinding(DS, "Table")
End Sub
End Class
Make the combined FirstName/LastName unique
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OleDb
public class CreateDataTableAndMakeCombinedColumnUnique
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim contacts_table As New DataTable("Contacts")
" Add columns.
contacts_table.Columns.Add("FirstName", GetType(String))
contacts_table.Columns.Add("LastName", GetType(String))
contacts_table.Columns.Add("Street", GetType(String))
contacts_table.Columns.Add("City", GetType(String))
contacts_table.Columns.Add("State", GetType(String))
contacts_table.Columns.Add("Zip", GetType(String))
" Make the combined FirstName/LastName unique.
Dim first_last_columns() As DataColumn = { _
contacts_table.Columns("FirstName"), _
contacts_table.Columns("LastName") _
}
contacts_table.Constraints.Add( _
New UniqueConstraint(first_last_columns))
" Make some contact data.
contacts_table.Rows.Add(New Object() {"A", "A", _
"1234", "B", "A", "11111"})
contacts_table.Rows.Add(New Object() {"B", "B", _
"22", "B", "C", "22222"})
contacts_table.Rows.Add(New Object() {"C", "C", _
"3", "P", "K", "33333"})
contacts_table.Rows.Add(New Object() {"", "D", _
"4", "P", "KS", "44444"})
grdAll.DataSource = contacts_table
grdAll.CaptionText = "All Records"
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form 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
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.SplitContainer1 = New System.Windows.Forms.SplitContainer
Me.SplitContainer2 = New System.Windows.Forms.SplitContainer
Me.grdAll = New System.Windows.Forms.DataGrid
Me.grdCO = New System.Windows.Forms.DataGrid
Me.grdName = New System.Windows.Forms.DataGrid
Me.SplitContainer1.Panel1.SuspendLayout()
Me.SplitContainer1.Panel2.SuspendLayout()
Me.SplitContainer1.SuspendLayout()
Me.SplitContainer2.Panel1.SuspendLayout()
Me.SplitContainer2.Panel2.SuspendLayout()
Me.SplitContainer2.SuspendLayout()
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"SplitContainer1
"
Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer1.Name = "SplitContainer1"
Me.SplitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer1.Panel1
"
Me.SplitContainer1.Panel1.Controls.Add(Me.grdAll)
"
"SplitContainer1.Panel2
"
Me.SplitContainer1.Panel2.Controls.Add(Me.SplitContainer2)
Me.SplitContainer1.Size = New System.Drawing.Size(519, 485)
Me.SplitContainer1.SplitterDistance = 180
Me.SplitContainer1.TabIndex = 2
Me.SplitContainer1.Text = "SplitContainer1"
"
"SplitContainer2
"
Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer2.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer2.Name = "SplitContainer2"
Me.SplitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer2.Panel1
"
Me.SplitContainer2.Panel1.Controls.Add(Me.grdCO)
"
"SplitContainer2.Panel2
"
Me.SplitContainer2.Panel2.Controls.Add(Me.grdName)
Me.SplitContainer2.Size = New System.Drawing.Size(519, 301)
Me.SplitContainer2.SplitterDistance = 173
Me.SplitContainer2.TabIndex = 0
Me.SplitContainer2.Text = "SplitContainer2"
"
"grdAll
"
Me.grdAll.DataMember = ""
Me.grdAll.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdAll.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdAll.Location = New System.Drawing.Point(0, 0)
Me.grdAll.Name = "grdAll"
Me.grdAll.Size = New System.Drawing.Size(519, 180)
Me.grdAll.TabIndex = 0
"
"grdCO
"
Me.grdCO.DataMember = ""
Me.grdCO.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdCO.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdCO.Location = New System.Drawing.Point(0, 0)
Me.grdCO.Name = "grdCO"
Me.grdCO.Size = New System.Drawing.Size(519, 173)
Me.grdCO.TabIndex = 1
"
"grdName
"
Me.grdName.DataMember = ""
Me.grdName.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdName.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdName.Location = New System.Drawing.Point(0, 0)
Me.grdName.Name = "grdName"
Me.grdName.Size = New System.Drawing.Size(519, 124)
Me.grdName.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(519, 485)
Me.Controls.Add(Me.SplitContainer1)
Me.Name = "Form1"
Me.Text = "Contacts"
Me.SplitContainer1.Panel1.ResumeLayout(False)
Me.SplitContainer1.Panel2.ResumeLayout(False)
Me.SplitContainer1.ResumeLayout(False)
Me.SplitContainer2.Panel1.ResumeLayout(False)
Me.SplitContainer2.Panel2.ResumeLayout(False)
Me.SplitContainer2.ResumeLayout(False)
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
Friend WithEvents grdAll As System.Windows.Forms.DataGrid
Friend WithEvents SplitContainer2 As System.Windows.Forms.SplitContainer
Friend WithEvents grdCO As System.Windows.Forms.DataGrid
Friend WithEvents grdName As System.Windows.Forms.DataGrid
End Class
Use DataView to filter data table
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OleDb
public class CreateDataTableAndFilteredByDataView
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim contacts_table As New DataTable("Contacts")
" Add columns.
contacts_table.Columns.Add("FirstName", GetType(String))
contacts_table.Columns.Add("LastName", GetType(String))
contacts_table.Columns.Add("Street", GetType(String))
contacts_table.Columns.Add("City", GetType(String))
contacts_table.Columns.Add("State", GetType(String))
contacts_table.Columns.Add("Zip", GetType(String))
contacts_table.Rows.Add(New Object() {"A", "A", _
"1234", "B", "A", "11111"})
contacts_table.Rows.Add(New Object() {"B", "B", _
"22", "B", "C", "22222"})
contacts_table.Rows.Add(New Object() {"C", "C", _
"3", "P", "K", "33333"})
contacts_table.Rows.Add(New Object() {"", "D", _
"4", "P", "KS", "44444"})
grdAll.DataSource = contacts_table
grdAll.CaptionText = "All Records"
Dim dv_co As New DataView(contacts_table)
dv_co.RowFilter = "State = "C""
grdCO.DataSource = dv_co
grdCO.CaptionText = "CO Records"
End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Public Class Form1
Inherits System.Windows.Forms.Form
"Form 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
"Required by the Windows Form Designer
Private components As System.ruponentModel.IContainer
"NOTE: 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.SplitContainer1 = New System.Windows.Forms.SplitContainer
Me.SplitContainer2 = New System.Windows.Forms.SplitContainer
Me.grdAll = New System.Windows.Forms.DataGrid
Me.grdCO = New System.Windows.Forms.DataGrid
Me.grdName = New System.Windows.Forms.DataGrid
Me.SplitContainer1.Panel1.SuspendLayout()
Me.SplitContainer1.Panel2.SuspendLayout()
Me.SplitContainer1.SuspendLayout()
Me.SplitContainer2.Panel1.SuspendLayout()
Me.SplitContainer2.Panel2.SuspendLayout()
Me.SplitContainer2.SuspendLayout()
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).BeginInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
"
"SplitContainer1
"
Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer1.Name = "SplitContainer1"
Me.SplitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer1.Panel1
"
Me.SplitContainer1.Panel1.Controls.Add(Me.grdAll)
"
"SplitContainer1.Panel2
"
Me.SplitContainer1.Panel2.Controls.Add(Me.SplitContainer2)
Me.SplitContainer1.Size = New System.Drawing.Size(519, 485)
Me.SplitContainer1.SplitterDistance = 180
Me.SplitContainer1.TabIndex = 2
Me.SplitContainer1.Text = "SplitContainer1"
"
"SplitContainer2
"
Me.SplitContainer2.Dock = System.Windows.Forms.DockStyle.Fill
Me.SplitContainer2.Location = New System.Drawing.Point(0, 0)
Me.SplitContainer2.Name = "SplitContainer2"
Me.SplitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal
"
"SplitContainer2.Panel1
"
Me.SplitContainer2.Panel1.Controls.Add(Me.grdCO)
"
"SplitContainer2.Panel2
"
Me.SplitContainer2.Panel2.Controls.Add(Me.grdName)
Me.SplitContainer2.Size = New System.Drawing.Size(519, 301)
Me.SplitContainer2.SplitterDistance = 173
Me.SplitContainer2.TabIndex = 0
Me.SplitContainer2.Text = "SplitContainer2"
"
"grdAll
"
Me.grdAll.DataMember = ""
Me.grdAll.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdAll.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdAll.Location = New System.Drawing.Point(0, 0)
Me.grdAll.Name = "grdAll"
Me.grdAll.Size = New System.Drawing.Size(519, 180)
Me.grdAll.TabIndex = 0
"
"grdCO
"
Me.grdCO.DataMember = ""
Me.grdCO.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdCO.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdCO.Location = New System.Drawing.Point(0, 0)
Me.grdCO.Name = "grdCO"
Me.grdCO.Size = New System.Drawing.Size(519, 173)
Me.grdCO.TabIndex = 1
"
"grdName
"
Me.grdName.DataMember = ""
Me.grdName.Dock = System.Windows.Forms.DockStyle.Fill
Me.grdName.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grdName.Location = New System.Drawing.Point(0, 0)
Me.grdName.Name = "grdName"
Me.grdName.Size = New System.Drawing.Size(519, 124)
Me.grdName.TabIndex = 1
"
"Form1
"
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(519, 485)
Me.Controls.Add(Me.SplitContainer1)
Me.Name = "Form1"
Me.Text = "Contacts"
Me.SplitContainer1.Panel1.ResumeLayout(False)
Me.SplitContainer1.Panel2.ResumeLayout(False)
Me.SplitContainer1.ResumeLayout(False)
Me.SplitContainer2.Panel1.ResumeLayout(False)
Me.SplitContainer2.Panel2.ResumeLayout(False)
Me.SplitContainer2.ResumeLayout(False)
CType(Me.grdAll, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdCO, System.ruponentModel.ISupportInitialize).EndInit()
CType(Me.grdName, System.ruponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer
Friend WithEvents grdAll As System.Windows.Forms.DataGrid
Friend WithEvents SplitContainer2 As System.Windows.Forms.SplitContainer
Friend WithEvents grdCO As System.Windows.Forms.DataGrid
Friend WithEvents grdName As System.Windows.Forms.DataGrid
End Class