私はエンティティのList <>にバインドされたComboBoxを使います。 「未選択」のエントリをコンボボックスに追加する方法を教えてください。リストにnullを追加すると、空のコンボボックスになります。
nullではなく、空の文字列または他の一意のテキストパターンを使用する必要があります。
そして、ComboboxのFormatイベントを処理して<empty>
をインターセプトして代替テキストを表示できます。
private void comboBox1_Format(object sender, ListControlConvertEventArgs e)
{
e.Value = FormatForCombobox(e.ListItem);
}
private string FormatForCombobox(object value)
{
string v = (string) value;
if (v == string.Empty)
v = "<no Selection>";
return v;
}
IEnumerable
エンティティリストにバインドしている場合は、空のオブジェクトを手動で確実に追加できます。
例えば
var qry = from c in Entities
select c;
var lst = qry.ToList();
var entity = new Entity();
entity.EntityId= -1;
entity.EntityDesc = "(All)";
lst.Insert(0, entity);
MyComboBox.DataSource = lst;
MyComboBox.DisplayMember = "EntityDesc"
MyComboBox.ValueMember = "EntityId"