2

My application's ComboBox, instead of displaying the specified member, "ProcName", is displaying the ToString() method results from that member's base class. The DataSource is defined as

List<ProcTemplateRecord> procList = dbif.GetProcTemplateRecords();

...where ProcTemplateRecord is my defined class:

class BaseRecord
{
    public Int32 PrimaryKey;
    public String SysTime;
}
class ProcTemplateRecord : BaseRecord
{
    public String ProcName;
    public String Comments;
}

In my application code, this is how I connect the ComboBox to my list:

this.comboBox1.DataSource = procList;
this.comboBox1.DisplayMember = "ProcName";
this.comboBox1.ValueMember = "PrimaryKey";

enter image description here

Any ideas on what I'm doing wrong?

4
  • 3
    You need to convert those from fields to properties Commented Jun 15, 2016 at 19:29
  • @Plutonix How is that done? This is new to me. Commented Jun 15, 2016 at 19:30
  • 2
    @Jim public int PrimaryKey {get; set;} public string ProcName {get; set;} Commented Jun 15, 2016 at 19:33
  • @Plutonix That did the trick, thanks! Feel free to post your answer, and I'll accept. :) Commented Jun 15, 2016 at 19:41

1 Answer 1

1

Its subtle, but when typing:

this.comboBox1.DisplayMember = "ProcName";

...notice the intellisense help: Gets or sets the property name for ..... Binding works on properties not fields which is what all your members are. Change them to properties and the binding should work:

class BaseRecord
{
    public Int32 PrimaryKey { get; set; }
    public String SysTime { get; set; }
}
class ProcTemplateRecord : BaseRecord
{
    public String ProcName { get; set; }
    public String Comments { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.