Why does my winforms command display the name of the object, and not the display element that I specify?

Here is the code:

cmbVegas.Items.AddRange((VegasPegasusCourseObject[])convertableCourses.ToArray()); cmbVegas.DisplayMember = "VegasCourseName"; cmbVegas.ValueMember = "CourseMapID"; 

convertableCourses is a List<VegasPegasusCourseObject>

Here I get a List from:

 public List<VegasPegasusCourseObject> GetConvertedCourses() { using (PanamaDataContext db = new PanamaDataContext()) { IQueryable<VegasPegasusCourseObject> vegasCourses = from cm in db.VegasToPegasusCourseMaps join c in db.Courses on cm.VegasCourseID equals c.CourseID join cp in db.Courses on cm.PegasusCourseID equals cp.CourseID select new VegasPegasusCourseObject { CourseMapID = cm.VPCMapID, VegasCourseName = c.CourseName, VegasCourseID = cm.VegasCourseID, PegasusCourseID = cm.PegasusCourseID, PegasusCourseName = cp.CourseName }; return vegasCourses.ToList(); } } 

Here is the obj def:

 class VegasPegasusCourseObject { public int CourseMapID; public string VegasCourseName; public int VegasCourseID; public string PegasusCourseName; public int PegasusCourseID; } 

However, when I fire this child, that’s all I get:

enter image description here

+4
source share
3 answers

According to the comments above, the problem is that "VegasCourseName" is written as a field, not a property. Therefore, an implementation of ToString was implemented instead.

Use the property instead:

 class VegasPegasusCourseObject { public string VegasCourseName { get; set;} } 
+9
source

From the docs for DisplayMember :

If the specified property does not exist in the object or the DisplayMember value is an empty string (""), the results of the ToString method of the object are displayed instead.

You do not have a property named "VegasCourseName" in VegasPegasusCourseObject and get a ClassName (by default instead of ToString ()).

+1
source

Override the ToString () method of the VegasPegasusCourseObject class

-2
source

Source: https://habr.com/ru/post/1381205/


All Articles