📄 form1.vb
字号:
Me.cmbCategory.TabIndex = 12
Me.cmbCategory.ValueMember = "CategoryID"
'
'DsCategories1
'
Me.DsCategories1.DataSetName = "DSCategories"
Me.DsCategories1.Locale = New System.Globalization.CultureInfo("en-US")
Me.DsCategories1.Namespace = "http://www.tempuri.org/DSCategories.xsd"
'
'cmbSupplier
'
Me.cmbSupplier.DataBindings.Add(New System.Windows.Forms.Binding("SelectedValue", Me.DsProducts1, "Products.SupplierID"))
Me.cmbSupplier.DataSource = Me.DsSuppliers1.Suppliers
Me.cmbSupplier.DisplayMember = "CompanyName"
Me.cmbSupplier.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.cmbSupplier.Location = New System.Drawing.Point(355, 150)
Me.cmbSupplier.Name = "cmbSupplier"
Me.cmbSupplier.Size = New System.Drawing.Size(169, 22)
Me.cmbSupplier.TabIndex = 13
Me.cmbSupplier.ValueMember = "SupplierID"
'
'DsSuppliers1
'
Me.DsSuppliers1.DataSetName = "DSSuppliers"
Me.DsSuppliers1.Locale = New System.Globalization.CultureInfo("en-US")
Me.DsSuppliers1.Namespace = "http://www.tempuri.org/DSSuppliers.xsd"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(219, 129)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(114, 18)
Me.Label3.TabIndex = 14
Me.Label3.Text = "Category"
'
'Label4
'
Me.Label4.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.Location = New System.Drawing.Point(355, 129)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(114, 18)
Me.Label4.TabIndex = 15
Me.Label4.Text = "Supplier"
'
'SqlDataAdapter2
'
Me.SqlDataAdapter2.SelectCommand = Me.SqlSelectCommand2
Me.SqlDataAdapter2.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Categories", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("CategoryID", "CategoryID"), New System.Data.Common.DataColumnMapping("CategoryName", "CategoryName")})})
'
'SqlSelectCommand2
'
Me.SqlSelectCommand2.CommandText = "SELECT CategoryID, CategoryName FROM Categories"
Me.SqlSelectCommand2.Connection = Me.SqlConnection1
'
'SqlDataAdapter3
'
Me.SqlDataAdapter3.SelectCommand = Me.SqlSelectCommand3
Me.SqlDataAdapter3.TableMappings.AddRange(New System.Data.Common.DataTableMapping() {New System.Data.Common.DataTableMapping("Table", "Suppliers", New System.Data.Common.DataColumnMapping() {New System.Data.Common.DataColumnMapping("SupplierID", "SupplierID"), New System.Data.Common.DataColumnMapping("CompanyName", "CompanyName")})})
'
'SqlSelectCommand3
'
Me.SqlSelectCommand3.CommandText = "SELECT SupplierID, CompanyName FROM Suppliers"
Me.SqlSelectCommand3.Connection = Me.SqlConnection1
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(530, 255)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label4, Me.Label3, Me.cmbSupplier, Me.cmbCategory, Me.Label2, Me.Label1, Me.bttnCancel, Me.bttnOK, Me.bttnDelete, Me.bttnNew, Me.ListBox1, Me.bttnUpdate, Me.bttnLoad, Me.TextBox3, Me.TextBox2, Me.TextBox1})
Me.Name = "Form1"
Me.Text = "Table Editing Demo"
CType(Me.DsProducts1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DsCategories1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.DsSuppliers1, System.ComponentModel.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
End Sub
Private Sub LoadData(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnLoad.Click
DsProducts1.Clear()
SqlDataAdapter1.Fill(DsProducts1, "Products")
DsCategories1.Clear()
SqlDataAdapter2.Fill(DsCategories1, "Categories")
DsSuppliers1.Clear()
SqlDataAdapter3.Fill(DsSuppliers1, "Suppliers")
End Sub
Private Sub UpdateTable(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnUpdate.Click
If Not DsProducts1.GetChanges(DataRowState.Modified) Is Nothing Then SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Modified))
If Not DsProducts1.GetChanges(DataRowState.Added) Is Nothing Then SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Added))
If Not DsProducts1.GetChanges(DataRowState.Deleted) Is Nothing Then SqlDataAdapter1.Update(DsProducts1.GetChanges(DataRowState.Deleted))
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Me.BindingContext(DsProducts1, "Products").EndCurrentEdit()
Try
Me.BindingContext(DsProducts1, "Products").Position = ListBox1.SelectedIndex
Catch exc As Exception
MsgBox(exc.Message)
End Try
End Sub
Private Sub bttnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnNew.Click
Me.BindingContext(DsProducts1, "Products").SuspendBinding()
ListBox1.Enabled = False
bttnOK.Visible = True
bttnCancel.Visible = True
bttnDelete.Visible = False
bttnNew.Visible = False
End Sub
Private Sub bttnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDelete.Click
DsProducts1.Products(Me.BindingContext(DsProducts1, "Products").Position).Delete()
End Sub
Private Sub bttnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnOK.Click
Dim newRow As DSProducts.ProductsRow
newRow = DsProducts1.Products.NewProductsRow
newRow.Item("ProductName") = TextBox2.Text
newRow.Item("UnitPrice") = TextBox3.Text
If cmbCategory.SelectedValue > 0 Then newRow.Item("CategoryID") = cmbCategory.SelectedValue
If cmbSupplier.SelectedValue > 0 Then newRow.Item("SupplierID") = cmbSupplier.SelectedValue
newRow.Item("Discontinued") = 0
DsProducts1.Products.AddProductsRow(newRow)
Me.BindingContext(DsProducts1, "Products").ResumeBinding()
ListBox1.Enabled = True
bttnOK.Visible = False
bttnCancel.Visible = False
bttnDelete.Visible = True
bttnNew.Visible = True
End Sub
Private Sub SqlDataAdapter1_RowUpdated(ByVal sender As Object, ByVal e As System.Data.SqlClient.SqlRowUpdatedEventArgs) Handles SqlDataAdapter1.RowUpdated
Select Case e.Row.RowState
Case DataRowState.Added
Console.Write("Adding Product ID = " & e.Row.Item("ProductID", DataRowVersion.Proposed))
Case DataRowState.Deleted
Console.Write("Deleting Product ID = " & e.Row.Item("ProductID", DataRowVersion.Original))
Case DataRowState.Modified
Console.Write("Updating Product ID = " & e.Row.Item("ProductID", DataRowVersion.Original))
End Select
If e.Status = UpdateStatus.ErrorsOccurred Then
Console.WriteLine("Failed to update row ")
Console.WriteLine(e.Errors.Message)
e.Row.RejectChanges()
Else
Console.WriteLine(" Updated successfully")
e.Row.AcceptChanges()
End If
End Sub
Private Sub bttnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnCancel.Click
Me.BindingContext(DsProducts1, "Products").ResumeBinding()
ListBox1.Enabled = True
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -