检查组合框c#.net Windows窗体的valuemember中的值

问题描述:

我有一个组合框,它的值成员具有以下值:AA,BB,CC,DD,EE.

I've a combobox and it's valuemember has the values : AA, BB, CC, DD, EE.

组合框Valuemember是从数据集中填充的.

Combobox Valuemember is populated from a dataset.

用户在文本框中输入一个值,我需要能够检查在文本框中输入的值是否是组合框Valuemember列表的一部分.

User enters a value in a textbox and I need to be able to check whether the value entered in the textbox is part of the combobox Valuemember list.

组合框填充如下:

OracleCommand oraCmd = new OracleCommand();
oraCmd.Connection = oraConn;
oraCmd.CommandType = CommandType.StoredProcedure;
oraCmd.CommandText = "sp1";

OracleCommandBuilder.DeriveParameters(oraCmd);
oraCmd.Parameters["Val1"].Value = "Val1";
oraCmd.Parameters["Val2"].Value = "Val2";

//Populate DataSet
OracleDataAdapter oraAdapter = new OracleDataAdapter(oraCmd);
DataSet oraDataSet = new DataSet();
oraAdapter.Fill(oraDataSet);

combobox1.ValueMember = oraDataSet.Tables[0].Columns["Val1"].ToString();
combobox1.DisplayMember = oraDataSet.Tables[0].Columns["Val2"].ToString();
combobox1.DataSource = oraDataSet.Tables[0].DefaultView;

我尝试了此方法,但是它不起作用:

I tried this but it does not work:

if (combobox1.ValueMember.Contains("XX"))
{
    combobox1.SelectedItem = "XX";
}
else
{
    combobox1.SelectedItem = "";
}

请帮助!

ValueMember DisplayMember 属性仅指定基础数据源中的哪些值应表示基础值每个项目的价值,并向用户显示该值.

The ValueMember and DisplayMember properties just specify which values in the underlying data source should represent the underlying value of each item, and the value displayed to the user.

尝试分配数据源,值和显示成员,如下所示:

Try assigning the data source, and value and display members, like this instead:

combobox1.DataSource = oraDataSet.Tables[0];
combobox1.ValueMember = "Val1";
combobox1.DisplayMember = "Val2";

然后尝试搜索所需的值,如下所示:

Then try searching for the desired value like this:

if (((DataTable)combobox1.DataSource).AsEnumerable()
                                     .Cast<DataRow>()
                                     .Select(x => Convert.ToString(x["Val1"]))
                                     .Contains("XX"))
{
    ...  // value found in combobox
}

您可以尝试使用其他语法:

Alternate syntax you can try:

if (comboBox1.Items.Cast<DataRowView>()
                   .Select(x => Convert.ToString(x["Val1"])
                   .Contains("XX"))
{
    ...
}