I am retrieving whole data from database in list?Now i have four class like:
Student class:
public class StudentTable
{
public string StudentID { get; set; }
public string StudentName { get; set; }
public StudentTable()
{
}
public StudentTable(string aStudentID, string aStudentName)
{
StudentID = aStudentID;
StudentName = aStudentName;
}
}
Course class
public class CourseTable
{
public string CourseID { get; set; }
public string CourseName { get; set; }
public int Credits { get; set; }
public CourseTable()
{
}
public CourseTable(string aCourseID, string aCourseName, int aCredits)
{
CourseID = aCourseID;
CourseName = aCourseName;
Credits = aCredits;
}
}
Enrollment class
public class Enrollment
{
public StudentTable StudentData { get; set; }
public CourseTable CourseData { get; set; }
public DateTime DOE { get; set; }
public Enrollment()
{
}
public Enrollment(StudentTable aStudentData, CourseTable aCourseData, DateTime aDOE)
{
StudentData = aStudentData;
CourseData = aCourseData;
DOE = aDOE;
}
}
StudentScore class:
public class StudentScore
{
public Enrollment EnrollmentData { get; set; }
public int Score { get; set; }
public StudentScore()
{
}
public StudentScore(Enrollment aEnrollmentData, int aScore)
{
EnrollmentData = aEnrollmentData;
Score = aScore;
}
}
and i retrieve the data from database through query now i want to display the StudentID from list but in the StudentScore Class i have EnorllmentData not StudentID.
try
{
var aStudentScore = EnrollmentDB.GetAllScore();
comboBox3.DataSource = aStudentScore;
comboBox3.DisplayMember = "StudentID";
comboBox3.ValueMember = "CourseID";
//comboBox3.ValueMember = "Score";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, ex.GetType().ToString());
}
It show me a DisplayMember error can you please help me to fix it, becaouse in StudentScore class have no StudentID variable so How can i display the StudentID from StudentScore Class?
So, first you had the Cannot bind to new display member error which Jon solved by showing you how to use dot-notation.
After that you are now getting the Unable to cast error. Without seeing that GetAllScore method I can’t say exactly why. However, you can do the heavy lifting yourself in code, and make the binding simple, by adding read-only properties to StudentScore to return what you want to use as the Display and Value Members. (I have to say that using StudentID as a DisplayMember and CourseID as a ValueMember doesn’t make any sense to me.)
Add something like this to your StudentScore:
Then set the data binding as:
This should get you working, but it feels more like a band-aid than a real fix.