绑定的组合框项目是指不同的字段项目 [英] Bound combobox items refers to different field items

查看:16
本文介绍了绑定的组合框项目是指不同的字段项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vb.net 中得到了一个组名的有界组合框.我可以在组合框的下拉项目中查看组名称集.但是当我执行插入或更新查询时,我需要使选定的组名指代 GROUP NUMBER.我不想在数据库中存储字母,而是我更喜欢数字.我该怎么做?!

I got a bounded combobox to a group name in vb.net. i can view in the drop down items of the combobox the set of GROUP NAMES. but when i do an insert or update query, i need to make the selected group name refers to the GROUP NUMBER. I don't want to store letters in the database, instead i prefer numbers. how can i do that?!

这是我目前的代码:

cmd1.Parameters.AddWithValue("@group", DirectCast(Additemcombobox.SelectedItem, 
                                  DataRowView).Item("GroupName"))

在数据库中存储组名目前运行良好.

Storing the group name in database is currently working well.

我的问题可能没有得到很好的解释.万一问我...任何帮助将不胜感激

My question might not be well explained. Please ask me in case... any help would be appreciated

推荐答案

您可以向用户显示一个元素,例如名称,但使用不同的元素作为代码以使用 DisplayMemberValueMember

You can show one element to the user such as the name, but use a different one for the code to identify the item using DisplayMember and ValueMember

Dim SQL = "SELECT Id, Name FROM GroupCode ORDER BY Name"
... 
GrpDT = New DataTable
GrpDT.Load(cmd.ExecuteReader)

cboGroup.DataSource = GrpDT
cboGroup.DisplayMember = "Name"
cboGroup.ValueMember = "Id"

用户只会看到名字,而代码可以使用ValueMember:

The user will only see the names, while the code can use ValueMember:

Private Sub cboGroup_SelectedValueChanged(...etc
    Console.WriteLine(cboGroup.SelectedValue)
End Sub

它打印的是组 ID 而不是名称.根据 SQL 中的 ORDER BY 子句和 ID,SelectedIndex 可能匹配也可能不匹配,因此响应 SelectedValueChanged 事件.如果您使用 SelectedValue 而不是 SelectedItem,您将不必纠结于 DataRowView 项目.

It prints the Group ID not the name. Depending on the ORDER BY clause in the SQL and the ID, the SelectedIndex may or may not match, so respond to SelectedValueChanged event. If you use SelectedValue instead of SelectedItem you wont have to thrash about with a DataRowView item.

请注意,SelectedValueObject,因此您必须将其转换为整数或任何其他地方使用的内容.

Note that SelectedValue is Object so you will have to cast to integer or whatever for use elsewhere.

这篇关于绑定的组合框项目是指不同的字段项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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