admin管理员组文章数量:1355555
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; }
}
本文标签: winformsWindows Form C ComboBox Display amp Value MemberStack Overflow
版权声明:本文标题:winforms - Windows Form C# ComboBox Display & Value Member - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744026485a2578134.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论