简体   繁体   English

VB.Net - Combobox 不显示数据

[英]VB.Net - Combobox Not Showing the Data

Combobox2.text not showing the called data from the table. Combobox2.text 未显示表中调用的数据。 Any help will be appreciated!任何帮助将不胜感激!

这是我正在处理的表格

Public Sub loadproductdata()

    Dim i As Integer

    str = "SELECT * FROM tbl_products INNER JOIN tbl_suppliers ON tbl_products.prod_supplier = tbl_suppliers.supp_ID WHERE prod_ID = '" & frm_ProductList.DataGridView1.SelectedRows(i).Cells(0).Value & "'"

    cmd = New MySqlCommand(str, con)
    con.Open()
    dr = cmd.ExecuteReader()

    If dr.Read() Then

        TextBox1.Text = (dr.Item("prod_code").ToString())
        TextBox2.Text = (dr.Item("prod_name").ToString())
        ComboBox1.Text = (dr.Item("prod_category").ToString())
        Label1.Text = (dr.Item("prod_supplier").ToString())
        ComboBox2.Text = (dr.Item("supp_name").ToString())
        TextBox3.Text = (dr.Item("prod_purchaseprice").ToString())
        TextBox4.Text = (dr.Item("prod_retailprice").ToString())
        TextBox5.Text = (dr.Item("prod_discount").ToString())
        ComboBox3.Text = (dr.Item("prod_unit").ToString())
        TextBox6.Text = (dr.Item("prod_stockqty").ToString())
        TextBox7.Text = (dr.Item("prod_reorderlvl").ToString())
        TextBox8.Text = (dr.Item("prod_description").ToString())
        TextBox9.Text = (dr.Item("prod_remarks").ToString())
   
    End If

    con.Close()

End Sub

When a combo box is in DropDownList mode, as yours appears to be, to set its current value to an item from its list you should set the SelectedValue property.当组合框处于 DropDownList 模式时,如您的那样,要将其当前值设置为其列表中的项目,您应该设置 SelectedValue 属性。 You should also consider filling the control with some kind of object that can maintain the difference between a key and a value items, to support a different display value versus backing value (supplier "Microsoft" has ID "6")您还应该考虑使用某种 object 填充控件,该控件可以保持键项和值项之间的差异,以支持不同的显示值与支持值(供应商“Microsoft”的 ID 为“6”)

While dr.Read()
    Dim kvp = new KeyValuePair(Of Integer, String) (0,"")
    kvp.Value= (dr.Item("supp_name").ToString())
    kvp.Key = DirectCast(dr.Item("supp_id"), Integer)

    ComboBox2.Items.Add(kvp)

Loop

Combobox2.DisplayMember = "Value"
Combobox2.ValueMember = "Key"

Now, if you set SelectedValue = 6 in your other query the combo shows "Microsoft"现在,如果您在其他查询中设置 SelectedValue = 6,则组合显示“Microsoft”

ps; ps; it's actually a lot easier if you just use a datatable for this:如果您为此使用数据表,实际上会容易得多:

Dim dt = new DataTable
Dim da as MySqlDataAdapter("SELECT * FROM...", "conn string here")
da.Fill(dt)
'rename your combobox2 to supplierComboBox
supplierComboBox.DataSource = dt
supplierComboBox.DisplayMember = "supp_name"
supplierComboBox.ValueMember = "supp_id"

And it gets easier still if you use strongly typed datasets everywhere instead of this intensely manual yanking data around - but that's out of scope of this question如果您在任何地方都使用强类型数据集而不是这种强烈的手动拉动数据,它会变得更容易 - 但这超出了这个问题的 scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM