最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

winforms - Windows Form C# ComboBox Display & Value Member - Stack Overflow

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论