Reputation: 101
I've a combobox and it's valuemember has the values : AA, BB, CC, DD, EE.
Combobox Valuemember is populated from a dataset.
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.
Combobox is populated as below:
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 = "";
}
Help please!
Upvotes: 0
Views: 1270
Reputation: 66439
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"))
{
...
}
Upvotes: 1