【发布时间】:2014-12-08 14:46:18
【问题描述】:
我很沮丧地完成了一个简单的组合框填充,下面我向组合框添加了一个新项目,一切似乎都很好。
问题 1:但是我怎样才能从 sql 查询中获取信息,而无需手动添加所有信息。 [我想通过简单地将 Items.Add 行添加到 while 循环],但这是另一件事 - 开始数据是数据库记录预览器,所以它是 Simple Name Simple Surname [/] 与所有客户的下拉菜单,
问题 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 =====================
好的,我的下拉列表已填充
正如我所说,这是一个记录预览表单,所以我现在如何获得组合框的默认选择。 问题是我传递了一个字符串
Form1.GridView1.GetRowCellValue(Form1.GridView1.FocusedRowHandle, "Wystawione_na").ToString()
此代码返回给我 SimpleName SimpleSurname - 作为一个字符串 同样的方法也适用于组合框显示。
我现在如何获取项目的 ID,它必须以某种方式比较并返回一个 ID,以便可以将 cmbx.SelectedIndex = 0 设置为客户选择的 ID
【问题讨论】:
-
要填充从一个类,按照我之前向您展示的操作 - 将它们添加到列表中并将列表用作绑定源。这比 Keith 的回答效率低得多,因为您基本上必须将所有数据带到 PC 上才能为列表构建一堆对象。
-
你的问题正在变成一个chameleon question,与原来的关系不大。如果您发现数据绑定有效,您应该标记 Keiths 的答案并提出您的新问题。也就是说,传递一个名字是一个非常糟糕的主意——你不能保证数据库中有一个“鲍勃史密斯”
-
@Plutonix 总是有名字所以这不是问题
-
我认为你没有抓住重点。您/您的代码不能保证会有一个且只有一个具有该名称的客户。这是数据库为记录分配唯一 ID 的全部原因。使用它。
-
他们不需要! ValueMember 将为您提供所选项目的 ID。绑定控件后,您可以获取实际数据(与其他数据副本的索引相比)。
标签: vb.net visual-studio-2010 combobox ado.net