如何设置ComboBox的SelectedIndex [英] How Set SelectedIndex for ComboBox

查看:844
本文介绍了如何设置ComboBox的SelectedIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sqlServer中有一个像这样的表:

ID标题说明
-----------------------------------
1名导演
3演员
5生产者

在我的项目中有2种形式:
1.活动
2. ActivityDialog

当我单击Edit_Click()时,将显示ActivityDialog.

ActivityDialog窗体中有一个DataBound组合框,其DisplayMember为标题",ValueMember为"ID".
在ActivityDialog_Load()处,我想为comboBox设置selectedIndex以显示当前记录的ActivityType.

我尝试过这些,但是它们都返回-1:

I have a table in sqlServer like this:

ID Title Description
-----------------------------------
1 Director
3 Actor
5 Producer

And in my project there is 2 Form:
1. Activity
2. ActivityDialog

When I Click on Edit_Click() the ActivityDialog will be shown.

There is a DataBound comboBox in ActivityDialog Form which its DisplayMember is "Title" and ValueMember is "ID".
At ActivityDialog_Load() I want to set selectedIndex for comboBox to show the current record''s ActivityType.

I have tried these but both of them returns -1:

comboBox.SelectedIndex = comboBox.Items.IndexOf(1);


还有


And

comboBox.SelectedIndex = comboBox.Items.IndexOf("Director");

推荐答案

尝试一下:
comboBox.SelectedIndex = 1;

并且组合框通常绑定到一个集合,因此请以插入的代码获取该集合中的索引.
Try this:
comboBox.SelectedIndex = 1;

and the combobox is usually boiunded to a collection, so get the index in the collection in code insted.


这篇关于如何设置ComboBox的SelectedIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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