How to remove values from valuemember in combobox?

980 Views Asked by At

The following code updates populates the combobox cmbBox1.

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

cmbBox1.ValueMember = oraDataSet.Tables[0].Columns["Val1"].ToString();

cmbBox1.DisplayMember = oraDataSet.Tables[0].Columns["Disp1"].ToString();
cmbBox1.DataSource = oraDataSet.Tables[0];

I need help in figuring out how to remove few values to from the cmbBox1. Do I remove values from the ValueMember/DisplayMember or is there any way to hide values in cmbBox1? Please advise

2

There are 2 best solutions below

2
On BEST ANSWER

You can use DataView

DataView dv = oraDataSet.Tables[0].DefaultView;
dv.RowFilter = "Code NOT IN (1,2,3)";

cmbBox1.ValueMember = oraDataSet.Tables[0].Columns["Val1"].ColumnName;

cmbBox1.DisplayMember = oraDataSet.Tables[0].Columns["Disp1"].ColumnName;
cmbBox1.DataSource = dv;
1
On

I suggest using OracleDataReader in following way

con.Open();
var cb1 = new OracleCommand(string, con);
OracleDataReader dr1 = cb1.ExecuteReader();

while (dr1.Read())
{
     cmbBox1.Items.Add(dr1["Val1"] + ", " + dr1["Val2"]);
     // no need to assign DisplayMember and ValueMember

}
dr1.Close();
dr1.Dispose();
con.Close();

For removing items that contain specific value you could use:

cmbBox1.Items.Remove("yourText");