vb.net 从 ComboBox 中选择值并在文本框中显示其详细信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17146807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:56:27  来源:igfitidea点击:

Select value from ComboBox and display it's details in textbox

databasevb.netcomboboxtextboxoledbconnection

提问by Milo Khoo

I'm having some problem to display product details from two tables.

我在显示两个表中的产品详细信息时遇到了一些问题。

I'm using VS 2010, and MS Access Database.

我正在使用 VS 2010 和 MS Access 数据库。

My database table structure are as follow :

我的数据库表结构如下:

  • Product (#Product_ID, Category_ID, Product_Name, Product_Cost, Product_Price)

  • Category (#Category_ID, Category_Name)

  • 乘积 ( #Product_ID, Category_ID, Product_Name, Product_Cost, Product_Price)

  • 类别 ( #Category_ID, Category_Name)

What I want is to display ProductCost, ProductPriceand CategoryNameinto Textbox when I select ProductNamefrom Combobox. I'm able to display ProductCost& ProductPricebut fail to display CategoryNamebecause I not sure how to make this two table link together.

当我从组合框选择时ProductCost,我想要的是显示,ProductPriceCategoryName进入文本框ProductName。我能够显示ProductCost&ProductPrice但无法显示,CategoryName因为我不确定如何将这两个表链接在一起。

The code I use to fill Combobox with ProductName is :

我用来用 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

Then I call the function this way on form load :

然后我在表单加载时以这种方式调用该函数:

fillProductCombobox(ProductComboBox)

This is my how my form look like : Form example

这是我的表格的样子: 表格示例

Please guide me how to I display CategoryNameas well.

请指导我如何显示CategoryName

Also is that the way I use to fill Product_Costand Product_Pricethe best method?

这也是我用来填充的方式Product_CostProduct_Price最好的方法吗?

P/S : For some reason I need to have everything done dynamically

P/S:出于某种原因,我需要动态完成所有工作

回答by Hitesh

You can use the join query like

您可以使用连接查询,如

 SELECT Product_ID, p.Category_ID, Product_Name, Product_Cost, Product_Price, Category_Name
 FROM Product p
 INNER JOIN Category c ON p.Category_ID = c.Category_ID

So, you will get the category name using this query, just bind the text box as you are binding for others.

因此,您将使用此查询获取类别名称,只需将文本框绑定为其他人绑定即可。

I hope it will help you. :)

我希望它会帮助你。:)