Hi omogen,
ComboBox’s items cannot be added or removed once it is data bound. Do the filter in the data source is a proper way. Please look at the following code.
dtComboBox.Columns.Add("Name");
dtComboBox.Columns.Add("Show");
dtComboBox.Rows.Add("John", true);
dtComboBox.Rows.Add("Mike", true);
dtComboBox.Rows.Add("%Kira", true);
foreach (DataRow dr in dtComboBox.Rows)
{
if (dr["Name"].ToString().Contains('%'))
{
dr["Show"] = false;
}
}
dtComboBox.DefaultView.RowFilter = "Show=true";
comboBox1.DataSource = dtComboBox;
comboBox1.DisplayMember = "Name";
dtComboBox is a datatable which will be used to do the databinding. In the example, I have added a column called “Show� At first, all the rows�Show value is set to true. Then put a loop to check if the name contains �� Set Show to false for the row whose name contains �� After that, you can use RowFilter to show the rows whose “Show�value is true.
If you have any problems with my solution, please feel free to tell me.
Sincerely,
Kira Qian
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the
All-In-One Code Framework!