2009-09-07 5 views

risposta

2

È necessario utilizzare una stringa vuota o un altro modello di testo univoco anziché null.

E quindi È possibile gestire l'evento Format di Combobox per intercettare lo <empty> e visualizzare un testo alternativo.

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; 
} 
+0

Non posso aggiungere nulla alla casella combinata, perché è legato alla lista delle entità. – wRAR

+0

È ancora possibile aggiungere un evento quando è associato. –

+0

Non sarà di aiuto perché non ho nulla di speciale da formattare. – wRAR

7

se siete legame IEnumerable elenco delle entità si può certamente aggiungere il tuo oggetto vuoto manualmente.

Per esempio

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" 
Problemi correlati