I have a ComboBox where the DisplayMember is a concatenated string of the value (SecSeniorityAccount) and description (SecSeniorityText) and the ValueMember is just the account.
When the user selects an item from the list, I would like ONLY the account to appear in the box, as I am reading the text property from the ComboBoxes when saving to DB. I can't get it to work. It is always showing the DisplayMember (concatenated string) in the box. I have even created a class to type it to be able to access the property I want.
The SelectionChangeCommitted event is firing and the MessageBox is outputting the correct value
Form load:
var srvcAccnt = await _ISDContext.IswSeniorityAccounts
.Select(a => new { a.SecSeniorityAccount, a.SecSeniorityText })
.ToListAsync();
var srvcAccntWithText = srvcAccnt.Select(a => new SeniorityAccountItem
{
SecSeniorityAccount = a.SecSeniorityAccount,
SecSeniorityText = a.SecSeniorityText,
DisplayText = $"{a.SecSeniorityAccount} - {a.SecSeniorityText}"
}).ToList();
cboSrvcAcct.DisplayMember = "DisplayText";
cboSrvcAcct.ValueMember = "SecSeniorityAccount";
cboSrvcAcct.DataSource = srvcAccntWithText;
cboSrvcAcct.SelectedIndex = -1;
cboSrvcAcct.SelectionChangeCommitted += (sender, e) =>
{
if (cboSrvcAcct.SelectedItem is SeniorityAccountItem selectedItem)
{
cboSrvcAcct.Text = selectedItem.SecSeniorityAccount;
MessageBox.Show(selectedItem.SecSeniorityAccount);
}
};
internal class SeniorityAccountItem
{
public string SecSeniorityAccount { get; set; }
public string SecSeniorityText { get; set; }
public string DisplayText { get; set; }
}