1

我有一堂课如下:

class villages { public string name; public int pId; }

我以这样的形式使用它:

private villages[] centerVillage=new villages[]{
        new villages{name= "village1",pId=0},
        new villages{name= "village2",pId=1},
        new villages{name= "village3",pId=2},
        new villages{name= "village4",pId=3},
        new villages{name= "village5",pId=4},
        new villages{name= "village6",pId=5},
        new villages{name= "village7",pId=6},
    };

现在我想combobox1villages[]DisplayMember=namevalueMember=pId

我已经尝试过了,但不起作用。

combobox1.DataSource = new BindingSource(centerVillage, null);
combobox1.DisplayMember = "name";
combobox1.ValueMember = "pId";
4

2 回答 2

4

在村庄类中,您必须定义属性以公开值,它不适用于归档成员:

    // Exceptions:
    //   System.ArgumentException:
    //     The specified property cannot be found on the object specified by the System.Windows.Forms.ListControl.DataSource
    //     property.
    public string ValueMember { get; set; }

这将解决问题:

    class villages
    {
        public string name { get; set; }
        public int pId { get; set; }
    }
于 2013-07-27T18:59:34.283 回答
1
combobox1.DataSource = villages;
combobox.DisplayMember = "name";
combobox1.ValueMember = "pId";
于 2013-07-27T18:50:44.217 回答