好的,所以这是我第一次尝试使用手动数据库表单而且我失败了...我不能为我的生活弄清楚为什么它没有填充网格视图或文本框。我有一个构建数据集的数据类,在Form Class中是控件的填充位置。
你能告诉我我做错了什么吗?我是数据库应用程序的菜鸟。这是我的数据类:
Option Strict Off
Option Explicit On
Imports System.Data
Public Class DataClass
Private CategoriesProductsDataSet As NORTHWNDDataSet
'Table adapters for product form
Private CategoriesTableAdapter As NORTHWNDDataSetTableAdapters.CategoriesTableAdapter
Private ProductTableAdapter As NORTHWNDDataSetTableAdapters.ProductsTableAdapter
'Declare data relation for the product form
Private CategoriesToProducts As DataRelation
'================================================================
'Create constructor for class
Public Sub New()
Try
With Me
'Instantiate the data sets
.CategoriesProductsDataSet = New NORTHWNDDataSet
'Instantiate the table adapters
.ProductTableAdapter = _
New NORTHWNDDataSetTableAdapters.ProductsTableAdapter
.CategoriesTableAdapter = _
New NORTHWNDDataSetTableAdapters.CategoriesTableAdapter
'Assign the products data relation to the dataset
.CategoriesToProducts = CategoriesProductsDataSet.Relations!CategoriesToProducts
'Fill the dataset using the fill method of the table adapters
.ProductTableAdapter.Fill(.CategoriesProductsDataSet.Products)
.CategoriesTableAdapter.Fill(.CategoriesProductsDataSet.Categories)
End With
Catch ex As Exception
MessageBox.Show("Error in data class", "error", MessageBoxButtons.OK)
End Try
End Sub
'================================================================
Public Function GetCategoriesProductsDataSet() As NORTHWNDDataSet
'Return the data set
Return CategoriesProductsDataSet
End Function
这是产品表格:
Option Strict Off
Option Explicit On
Imports System.Data
'Module: ProductForm
'Abstract: This form displays all product information from the database
'for the employee to edit or add to.
Public Class ProductForm
'Declare module-level variables
Private CategoriesProductsData As DataClass
Private CategoriesProductsDataSet As NORTHWNDDataSet
Private CategoriesTableAdapter As _
NORTHWNDDataSetTableAdapters.ProductsTableAdapter
Private ProductsTableAdapter As _
NORTHWNDDataSetTableAdapters.CategoriesTableAdapter
这两行被注释掉了,因为它告诉我已经为这两个绑定源创建了Friends WithEvents。虽然我找不到它们。
'Private WithEvents ProductsBindingSource As BindingSource
'Private WithEvents CategoriesBindingSource As BindingSource
Private AddingBoolean As Boolean
Private ClosingBoolean As Boolean
Private EditingBoolean As Boolean
Private GridInitializedBoolean As Boolean
Private CategoryIDString As String 'holds the category id to filter the data
'===============================================================
Private Sub ProductForm_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
这段代码产生了错误:
Try
CategoriesProductsData = New DataClass
CategoriesProductsDataSet = New NORTHWNDDataSet
CategoriesProductsDataSet = _
CategoriesProductsData.GetCategoriesProductsDataSet
'Set up binding sources
ProductsBindingSource = New BindingSource
CategoriesBindingSource = New BindingSource
'Set the data source and data member
With CategoriesBindingSource
.DataSource = CategoriesProductsDataSet
.DataMember = "Products"
.Sort = "CategoryID"
End With
With ProductsBindingSource
.DataSource = CategoriesBindingSource
.DataMember = "ProductsToCategories"
End With
Catch ex As Exception
MessageBox.Show("Database Error", "Error", MessageBoxButtons.OK)
End Try
它会抛出我的消息框,所以我假设它没有正确连接到数据库。
'Establish record count
CategoriesBindingSource.MoveLast()
CategoriesBindingSource.MoveFirst()
'Bind the textboxes
CategoryIDTextBox.DataBindings.Add("text", _
CategoriesBindingSource, "CategoryID")
CategoryNameTextBox.DataBindings.Add("text", _
CategoriesBindingSource, "CategoryName")
DescripRichTextBox.DataBindings.Add("text", _
CategoriesBindingSource, "Description")
'Initialize binding for Products data grid view
If Not GridInitializedBoolean Then
'Bind and format the grid
ProductsDataGridView.DataSource = _
ProductsBindingSource
SetUpGridColumns()
GridInitializedBoolean = True
End If
'Filter the products by category ID
ProductsBindingSource.Filter = "CategoryID = '" & _
CategoryIDTextBox.Text & "'"
End Sub
'===============================================================
'Create a sub routine to set up the grid columns and set the
'column widths.
Private Sub SetUpGridColumns()
Try
With Me.ProductsDataGridView
'Set up column headers
.Columns!category_id.HeaderText = "CategoryID"
.Columns!product_id.HeaderText = "ProductID"
.Columns!prodName.HeaderText = "Name"
.Columns!supplier_id.HeaderText = "SupplierID"
.Columns!quantity.HeaderText = "Quantity/Unit"
.Columns!unit_price.HeaderText = "Unit Price"
.Columns!in_stock.HeaderText = "Units In Stock"
.Columns!on_order.HeaderText = "Units Ordered"
.Columns!reorder.HeaderText = "Reorder Level"
.Columns!discontinued.HeaderText = "Discontinued"
'Set up column widths
.Columns!category_id.Width = 100
.Columns!product_id.Width = 75
.Columns!prodName.Width = 30
.Columns!lsupplier_id.Width = 75
.Columns!quantity.Width = 35
.Columns!unit_price.Width = 50
.Columns!in_stock.Width = 75
.Columns!reorder.Width = 90
End With
Catch ex As Exception
End Try
End Sub
End Class
答案 0 :(得分:0)
发现错误!这是一个简单的错误。
我有:
With ProductsBindingSource
.DataSource = CategoriesBindingSource
.DataMember = "ProductsToCategories"
End With
应该是:(使用绑定源而不是数据集):/
With ProductsBindingSource
.DataSource = CategoriesProductsDataSet
.DataMember = "products"
.Sort = "ProductID"