2

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

user1100941
  • 101
  • 1
  • 11
  • Remove the row from `oraDataSet.Tables[0].Rows.RemoveAt(cmbBox1.SelectedIndex);` – LarsTech Jun 09 '14 at 21:38
  • Is it possible to remove the rows from the combobox and not the dataset? – user1100941 Jun 09 '14 at 21:50
  • Don't use a DataSource. `cmbBox1.Items.Add(...);` But I don't know why you want to avoid removing the rows from the table. – LarsTech Jun 09 '14 at 21:57
  • Users see complete text on the drop down: Text1, Text2, Text3 and when they select an item, selecteditem is read back and saved as short text eg: T1, T2, T3. I guess I'll have to declare oraDataSet outside of the method and make changes. Thanks @LarsTech – user1100941 Jun 09 '14 at 22:04

2 Answers2

5

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;
fairybot
  • 114
  • 5
1

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");
Marek
  • 3,555
  • 17
  • 74
  • 123
  • I tried using your approach. I want to display Val2 in the combobox drop down and when the user chooses an item from the combobox, I want to match it with Val1 and save the values in Val1. The approach you suggested displays the items as "Val1 Val2". Thanks – user1100941 Jun 10 '14 at 15:11