组合框SelectedValue始终是一个对象

时间:2019-02-26 02:35:31

标签: c# .net

这就是我声明组合框的方式

public void RefreshList()
{
    List<DropdownList> data = new List<DropdownList>();
    RecursiveClearInputs(this.Controls);
    var programs = programsData.GetAll();
    data.Add(new DropdownList
    {
        Id = "-1",
        Name = "Select"
    });
    foreach (var item in programs)
    {
        data.Add(new DropdownList {
            Id = item.Id,
            Name = item.Name
        });
    }
    programInput.DataSource = data;
    programInput.DisplayMember = "Name";
    programInput.ValueMember = "Id";
    programInput.SelectedIndex = 0;
}

页面加载时,将刷新列表。但是,选择一个项目后,如何从下拉列表中获取ID部分?它始终显示为一个对象。我尝试将其放到toString(),但它打印的只是我的自定义对象结构/视图模型。

这是我的下拉选择代码:

var selected = programInput.SelectedValue.ToString();
if (selected != "-1")
{
    var result = programsData.Get(selected.ToString());
    if (result != null)
    {
        nameInput.Text = result.Name;
        totalFieldsInput.Text = result.Fields.ToString();
        filetypeInput.Text = result.Filetype;
        descriptionInput.Text = result.Description;
        delimiterInput.Text = result.Delimiter;
        IdInput.Text = result.Id;
    }
}

0 个答案:

没有答案