C# 无法以编程方式设置组合框选择

C# 无法以编程方式设置组合框选择,c#,.net,winforms,C#,.net,Winforms,我无法以编程方式在组合框中设置选择。 我尝试设置各种属性SelectedItem、SelectedText、SelectedIndex,但组合框不显示名称。选择组合框的第一行,该行为空。属性设置的返回值是正确的。 我做错了什么 ... this.bsConstructionContractors.DataSource = typeof(Contractor); ... public partial class EditContractorDialog : Form { public Ed

我无法以编程方式在组合框中设置选择。 我尝试设置各种属性SelectedItem、SelectedText、SelectedIndex,但组合框不显示名称。选择组合框的第一行,该行为空。属性设置的返回值是正确的。 我做错了什么

...
this.bsConstructionContractors.DataSource = typeof(Contractor);
...
public partial class EditContractorDialog : Form
{
    public EditContractorDialog(Contractor contractor)
    {
        InitializeComponent();

        this.cmbEditContractorName.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", projectView.bsProject, "ContractorId", true));
        // new BindingSource is important here, so as to use a separate CurrencyManager
        // because bsConstructionContractors is shared with another ComboBox on another form
        this.cmbEditContractorName.DataSource = new BindingSource(projectView.bsConstructionContractors, null);
        this.cmbEditContractorName.ValueMember = "ContractorId";
        this.cmbEditContractorName.DisplayMember = "Name";

         cmbEditContractorName.Enabled = true;
         cmbEditContractorName.Focus();

        // None of the following cause the ComboBox to display Contractor.Name
        // The first entry in the ComboBox, which is a blank, is displayed
        object myObject = cmbEditContractorName.SelectedItem = contractor; 
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject: " + myObject.GetType()); // type is Contractor
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject: " + myObject); // myObject is the contractor

        object myObject2 = cmbEditContractorName.SelectedText = contractor.Name;
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject2: " + myObject2.GetType()); // type is String
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject2: " + myObject2); // myObject2 is the contractor.Name

        object myObject3 = cmbEditContractorName.SelectedIndex = 3; // arbitrary index, just to see if it would be selected
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject3: " + myObject3.GetType()); // type is Int32
        System.Diagnostics.Trace.WriteLine("EditContractorDialog(): myObject3: " + myObject3); // myObject3 = 3
     }
}

public partial class Contractor : System.IComparable
{
    protected int id;
    protected string name;
    protected string licenseNumber;
    protected Project project;

    public virtual int ContractorId
    {
        get { return this.id; }
        set { this.id = value; }
    }

    public virtual string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    public virtual string LicenseNumber
    {
        get { return this.licenseNumber; }
        set
        { this.licenseNumber = value; }
    }

    public virtual int CompareTo(object obj)
    {
        if (!(obj is Contractor))
        {
            throw new System.InvalidCastException("This object is not of type Contractor");
        }
        return this.Name.CompareTo(((Contractor)obj).Name);
    }

}

使用SelectedIndex按承包商名称查找索引:

cmbEditContractorName.SelectedIndex = cmbEditContractorName.FindString(contractor.Name);

使用SelectedIndex按承包商名称查找索引:

cmbEditContractorName.SelectedIndex = cmbEditContractorName.FindString(contractor.Name);

这是因为您需要从combobox本身的数据源获取要分配的对象引用。 对于组合框,我建议您使用ObservableCollection


代码不起作用,因为您正在使用combobox数据源中不存在的对象引用来分配SelectedItem属性。

这是因为您需要从combobox本身的数据源获取要分配的对象引用。 对于组合框,我建议您使用ObservableCollection


您的代码不起作用,因为您正在使用combobox数据源中不存在的对象的引用来分配SelectedItem属性。

转到与cmbEditContractorName相关的InitializeComponent和paste部分基本上所有与该变量相关的行。感谢您的评论。我搬家了。cmbEditContractorName。数据绑定。。。。通过将“.cmbEditContractorName.Enabled…”传递给设计器,但这没有任何效果。虽然它认为它们在设计器中更好,但基于您的设置,为SelectedValue指定有效值就足够了。此外,它还应该在最初显示您正在编辑的项目的承包商。@AlLelopath您不理解我的评论。我希望您将该组合框的设计器配置粘贴到您的问题中。很抱歉,我误解了。无论如何,下面有两个答案,都是有效的。请转到InitializeComponent并粘贴与cmbEditContractorName相关的部分,基本上所有与该变量相关的行。感谢您的评论。我搬家了。cmbEditContractorName。数据绑定。。。。通过将“.cmbEditContractorName.Enabled…”传递给设计器,但这没有任何效果。虽然它认为它们在设计器中更好,但基于您的设置,为SelectedValue指定有效值就足够了。此外,它还应该在最初显示您正在编辑的项目的承包商。@AlLelopath您不理解我的评论。我希望您将该组合框的设计器配置粘贴到您的问题中。很抱歉,我误解了。无论如何,下面有两个答案,都是有效的。