使用类填充组合框 - 来自SQL查询的数据

时间:2014-12-08 14:46:19

标签: vb.net visual-studio-2010 combobox ado.net

我很沮丧地完成了一个组合框的简单填充,下面我在组合框中添加了一个新项目似乎一切都很好。

问题1:但是如何从sql查询中获取信息,而无需手动添加所有信息。 [我想通过简单地将Items.Add行添加到while循环],但这是另一回事 - 开始数据是数据库记录预览器,因此它是简单名称简单姓氏[/],带有所有客户的下拉菜单,< / p>

enter image description here

问题2.我从mysql结果中获得的数据是id,name,surname如何将其指向当前显示的名称/姓氏以及用于以后的目的 - 比如更新从下拉列表中获取另一个客户的选定ID?我不需要插入命令或代码只需要信息如何从选择中获取id。如果有什么不清楚的地方,请不要犹豫。

'Select the first item ( the selection would be a ID of the customer which isn't the index at all)
ComboBoxEdit.SelectedIndex = 0

表格

Dim properties As DevExpress.XtraEditors.Repository.RepositoryItemComboBox = _
            ComboBoxEdit.Properties
properties.Items.Add(New Customers(1, "Ta", "t").ToString)
'Select the first item ( the selection would be a ID of the customer which isn't the index at all)
ComboBoxEdit.SelectedIndex = 0

让客户进入类客户(我想我需要这样做)

Public Function init_customers()
        ' Create a list of strings.
        Dim sql As String
        Dim myReader As MySqlDataReader

        con.Open()
        sql = "select * from customers"
        'bind the connection and query
        With cmd
            .Connection = con
            .CommandText = sql
        End With
        myReader = cmd.ExecuteReader()
        While myReader.Read()
            list.Add(New Customers(myReader.GetInt64(0), myReader.GetString(1), myReader.GetString(2)))
        End While
        con.Close()
        'Return list
    End Function

班级客户

Public Class Customers

    Public Sub New(ByVal id As Integer, ByVal name As String, ByVal surname As String)
        Me.ID = id
        Me.Imie = name
        Me.Nazwisko = surname

    End Sub
#Region "Get/Set"
    Public Property ID() As Integer
        Get
            Return Me._id
        End Get
        Set(ByVal value As Integer)
            Me._id = value
        End Set
    End Property
    Public Property Imie() As String
        Get
            Return Me._imie
        End Get
        Set(ByVal value As String)
            Me._imie = value
        End Set
    End Property
    Public Property Nazwisko() As String
        Get
            Return Me._nazwisko
        End Get
        Set(ByVal value As String)
            Me._nazwisko = value
        End Set
    End Property
    Public ReadOnly Property Surname() As Decimal
        Get
            Return Me._nazwisko
        End Get
    End Property
    Public Overrides Function ToString() As String
        Return _imie + " " + _nazwisko
    End Function
#End Region
    Private _id As Integer
    Private _imie As String
    Private _nazwisko As String

End Class

===========编辑2 =====================

好的我的下拉列表已填充

enter image description here

正如我所说,这是一个记录预览表格,所以我现在如何获得组合框的默认选择。 事情是我在那里传递一个字符串

Form1.GridView1.GetRowCellValue(Form1.GridView1.FocusedRowHandle, "Wystawione_na").ToString()

此代码返回SimpleName SimpleSurname - 作为一个字符串 相同的方法应用于组合框显示。

如何获得项目的ID,它必须以某种方式比较并返回一个id,以便可以将cmbx.SelectedIndex = 0设置为客户选择的ID

1 个答案:

答案 0 :(得分:2)

我采取了一条更简单的路线,但不确定它是否最有效:

Dim Conn As New SqlConnection
Conn.ConnectionString = sYourConnectionString
Conn.Open()

Dim da As New SqlDataAdapter("select * from customers", Conn)
Dim ds As New DataSet
da.Fill(ds, sSql)

cmbxCustomers.DataSource = ds.Tables(0)
cmbxCustomers.ValueMember = "ID" 'or whatever column you want

当然,我通常使用包装类来完成上述几乎所有代码,但最后两行适用于您的部分问题。

基于所选ID,稍后检索该数据:您可以简单地使用DataSet(或DataTable,我的首选项)类成员变量,以便从初始加载中存储数据并遍历表中查找与您想要信息的ID匹配的行。