gpt4 book ai didi

mysql - 在 ComboBox.Item 中隐藏部分 String.Format

转载 作者:行者123 更新时间:2023-11-30 22:00:14 25 4
gpt4 key购买 nike

是否可以隐藏部分String.Format

这是我的代码:

'Select Product'
Try
MysqlConn.Close()
MysqlConn.Open()
Dim Query As String
Query = "select id, name,id_maker, id_types from product ORDER BY name ASC"
COMMAND = New MySqlCommand(Query, MysqlConn)
READER = COMMAND.ExecuteReader
While READER.Read
Dim sName = READER.GetString("name")
Dim sMaker = READER.GetString("id_maker")
Dim sTypes = READER.GetString("id_types")
Dim sId = READER.GetString("id")

'ComboBox1.Items.Add(sName)'
ComboBox1.Items.Add(String.Format("{0}|{1}|{2}|{3}", sName, sMaker, sTypes, sId))


End While
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
'Select Product'

我想隐藏 ComboBox 中的 sId {3},因为稍后我需要使用 ComboBox1.Text 的查询> 被使用,id 是必需的。

最佳答案

也许您可以更改将数据分配给 ComboBox 的方式。

首先要做的是更改查询并使用 CONCAT :

SELECT id, CONCAT(name,'|',id_maker,'|',id_types) AS value FROM product ORDER BY name ASC

我也会实现 Using :

Managed resources are disposed of by the .NET Framework garbage collector (GC) without any extra coding on your part. You do not need a Using block for managed resources. However, you can still use a Using block to force the disposal of a managed resource instead of waiting for the garbage collector.

您也不需要READER。而是将数据加载到 DataTable 并将其分配给 ComboBox 上的 .DataSource 属性。

你的代码看起来像这样:

Using con As New MySqlConnection(connectionString)
cmd As New MySqlCommand("SELECT id, CONCAT(name,'|',id_maker,'|',id_types) AS value FROM product ORDER BY name ASC", con)

con.Open()

Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())

ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "value"
ComboBox1.ValueMember = "id"
End Using

您现在可以使用这段代码获取 id:

ComboBox1.SelectedValue.ToString()

您可以使用这段代码获取文本:

ComboBox1.Text

关于mysql - 在 ComboBox.Item 中隐藏部分 String.Format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43611082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com