【发布时间】:2013-06-17 20:07:17
【问题描述】:
我在显示两个表格中的产品详细信息时遇到了一些问题。
我正在使用 VS 2010 和 MS Access 数据库。
我的数据库表结构如下:
产品(
#Product_ID、Category_ID、Product_Name、Product_Cost、Product_Price)类别(
#Category_ID、Category_Name)
当我从组合框中选择ProductName 时,我想要在文本框中显示ProductCost、ProductPrice 和CategoryName。我可以显示ProductCost 和ProductPrice,但无法显示CategoryName,因为我不确定如何将这两个表链接在一起。
我用 ProductName 填充 Combobox 的代码是:
Public Sub fillProductCombobox(ByVal sender As Object)
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Try
conn.Open()
da.SelectCommand = New OleDbCommand("SELECT * FROM Product", conn)
da.Fill(dt)
sender.DataSource = dt
sender.DisplayMember = "Product_Name"
sender.ValueMember = "Product_ID"
'best method?
frmAddSalesProduct.txtProductCost.DataBindings.Add("Text", dt, "Product_Cost")
frmAddSalesProduct.txtPerPrice.DataBindings.Add("Text", dt, "Product_Price")
Catch ex As Exception
MsgBox(ex.Message)
Finally
conn.Close()
End Try
End Sub
然后我在表单加载时以这种方式调用函数:
fillProductCombobox(ProductComboBox)
这是我的表单的样子:
请指导我如何显示CategoryName。
这也是我用来填充Product_Cost和Product_Price的最好方法吗?
P/S : 出于某种原因,我需要动态完成所有操作
【问题讨论】:
-
你的结构是
ProductID,但你绑定到Product_ID。 -
啊抱歉是 Product_ID 我在这里弄错了 :) 进行了编辑。
标签: database vb.net combobox textbox oledbconnection