Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c在组合框中选择一个值_C#_Database_Select_Combobox - Fatal编程技术网

C# c在组合框中选择一个值

C# c在组合框中选择一个值,c#,database,select,combobox,C#,Database,Select,Combobox,c windows窗体中的我的组合框中填充了我数据库中的数据。。。 显示成员是字符串,值成员是整数 我现在必须在显示表单之前预选它。 我试过: combobox.DisplayMember = string; combobox.Text = string; combobox.SelectedItem = string; combobox.SelectedText = string; combobox.SelectedValue = string; 有谁能帮我一点忙吗? 将非常感谢:- 编辑:e

c windows窗体中的我的组合框中填充了我数据库中的数据。。。 显示成员是字符串,值成员是整数

我现在必须在显示表单之前预选它。 我试过:

combobox.DisplayMember = string;
combobox.Text = string;
combobox.SelectedItem = string;
combobox.SelectedText = string;
combobox.SelectedValue = string;
有谁能帮我一点忙吗? 将非常感谢:-

编辑:ei。也许其他人的解决方案。。。 请记住,VS2010设计器创建的负载是在构造函数之后加载的。正如我所想,不是在initializeComponents中

使用ComboBox.SelectedIndex

例如:

请注意,这是一个,它有一个名为的方法。向它传递一个要选择的对象的引用,您应该会得到正确的索引。

使用ComboBox.SelectedIndex

例如:

请注意,这是一个,它有一个名为的方法。向它传递一个对要选择的对象的引用,您应该会获得正确的索引。

找到该项,然后将combobox的SelectedItem属性设置为true

编辑:

找到该项,然后将combobox的SelectedItem属性设置为true

编辑:


如果组合框是数据绑定的,并且已正确设置DisplayMember和ValueMember属性,则只需将SelectedValue属性设置为要选择的项的值

例如,如果组合框中有以下对象:

ID Description -- ----------------- 2 Lorem 4 Ipsum 6 Dolor 8 Sit
您可以将DisplayMember设置为Description,将ValueMember设置为ID。然后,为了选择项目Dolor,您只需将SelectedValue设置为6。

如果您的组合框是数据绑定的,并且您已经正确设置了DisplayMember和ValueMember属性,然后,您只需将SelectedValue属性设置为要选择的项目的值

例如,如果组合框中有以下对象:

ID Description -- ----------------- 2 Lorem 4 Ipsum 6 Dolor 8 Sit 您可以将DisplayMember设置为Description,将ValueMember设置为ID。然后,为了选择项目Dolor,您只需将SelectedValue设置为6。

另一种方式:

combobox.SelectedIndex = combobox.FindStringExact("myString")
另一种方式:

combobox.SelectedIndex = combobox.FindStringExact("myString")


没有。首先在我的项目下,你知道为什么吗-将System.Linq命名空间添加到您拥有的usings.using System;使用System.Collections.Generic;使用系统组件模型;使用系统数据;使用系统图;使用System.Linq;使用系统文本;使用System.Windows.Forms\n这些是我的最新作品吗?呵呵,非常感谢你的回答,它们都很好地发挥了作用。不过,在你上次编辑后没有进行测试。+1,我使用了:comboBox.SelectedItem=comboBox.Items.Cast.Firsto=>o.Text==blala;没有。首先在我的项目下,你知道为什么吗-将System.Linq命名空间添加到您拥有的usings.using System;使用System.Collections.Generic;使用系统组件模型;使用系统数据;使用系统图;使用System.Linq;使用系统文本;使用System.Windows.Forms\n这些是我的最新作品吗?呵呵,非常感谢你的回答,它们都很好地发挥了作用。不过,在你上次编辑后没有进行测试。+1,我使用了:comboBox.SelectedItem=comboBox.Items.Cast.Firsto=>o.Text==blala;如果我这么愚蠢地问,我该怎么得到索引呢-@Anders Metnik嗯,一种方法是使用ComboBox.Items.IndexOf。请参见编辑。如果我问得这么蠢,我该如何获取索引-@Anders Metnik嗯,一种方法是使用ComboBox.Items.IndexOf。请参见编辑。但我没有值,只有字符串:我使用visual studio,到目前为止对我很好:-然后查看FindString方法,但我没有值,只有字符串:我使用visual studio,到目前为止对我很好:-然后查看FindString方法抱歉,朋友,再次检查,如果有任何问题,请发表评论。对不起,朋友,请再次检查,如果有任何问题,请发表评论。
combobox.SelectedIndex = combobox.FindStringExact("myString")