显示数据从MYSQL数据库到控件,特别是在组合框是DropDown [英] Displaying Data from MYSQL Database to Controls especially on Combobox that is DropDown

查看:215
本文介绍了显示数据从MYSQL数据库到控件,特别是在组合框是DropDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好!我在VB.Net中有一个代码,将数据从数据库传输到表单控件,如textbox和combobox。它工作完美除了一个。

Good Morning!, I have a code in VB.Net that transfer data from database into form controls like textbox and combobox. It works perfectly except for one. It doesnt display the data in the combobox.

  Dim con As MySqlConnection = New MySqlConnection("server=192.168.2.246;userid=root;password=admin1950;database=inventory")
        Dim cmd As MySqlCommand = New MySqlCommand("select RIDate,DateReceived,Status,PreparedBy,Location,Supplier,TotalAmount,GeneralRemarks,DocNo,CRFNo,RecBy,DATE_FORMAT(PrintDate,'%m/%d/%Y')as PrintDate from receiving where RINo = '" & TextBox1.Text & "'", con)
        Dim reader As MySqlDataReader
        con.Open()
        reader = cmd.ExecuteReader
        While reader.Read
            DateTimePicker1.Value = reader.GetString("RIDate")
            DateTimePicker2.Value = reader.GetString("DateReceived")
            TextBox2.Text = reader.GetString("Status")
            TextBox3.Text = reader.GetString("PreparedBy")
            ComboBox1.Text = reader.GetString("Location")   <----------This 
            ComboBox2.Text = reader.GetString("Supplier")   <----------is the Problem
            Try
                TextBox4.Text = reader.GetString("PrintDate")
            Catch
                TextBox4.Text = ""
            End Try
            TextBox6.Text = reader.GetString("TotalAmount")
            Try
                TextBox7.Text = reader.GetString("GeneralRemarks")
            Catch ex As Exception
                TextBox7.Text = ""
            End Try
            TextBox11.Text = reader.GetString("DocNo")
            Try
                TextBox9.Text = reader.GetString("CRFNo")
            Catch
                TextBox9.Text = ""
            End Try
            Try
                TextBox10.Text = reader.GetString("RecBy")
            Catch
                TextBox10.Text = ""
            End Try
        End While
        con.Close()

它的工作,但当我将组合框更改为Dropdownlist数据不显示了。

Its working but when I change the combobox into Dropdownlist the data doesnt show any more. what do you think is the prob?

TYSM帮助

推荐答案

DropDownList 样式确保用户无法选择不在列表中的元素。请确保 reader.GetString(Location)的值位于 ComboBox1 的数据源中。

The DropDownList style ensures that users cannot select an element that isn't in the list. Make sure the value of reader.GetString("Location") is in ComboBox1's data source.

如果数据源是字符串列表,那么这应该可以正常工作。如果是使用 DisplayMember ValueMember 绑定的对象列表,则 .Text 属性将与 DisplayMember 字段匹配。

If the datasource is a list of strings then this should work fine. If it's a list of objects where you are binding using DisplayMember and ValueMember then the .Text property will match against the DisplayMember field.

例如,如果您有一个对象 {。Name =New York City,.Value =NYC} 并且ComboBox有 DisplayMember =Name ValueMember =Value。那么 .Text 需要纽约市才能匹配。如果您需要在 NYC 上匹配,而不要设置 .SelectedValue =NYC

For instance, if you have an object with {.Name = "New York City", .Value = "NYC"} and the ComboBox has DisplayMember = "Name" ValueMember = "Value". Then .Text needs to be New York City to match. If you need to match on NYC instead set .SelectedValue = "NYC"

这篇关于显示数据从MYSQL数据库到控件,特别是在组合框是DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆