我从列表中的数据库中检索整个数据?现在我有四个类:
学生班:
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;
}
}
课程班
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;
}
}
报名课
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课程:
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;
}
}
我通过查询从数据库中检索数据现在我想从列表中显示StudentID但在StudentScore类中我有EnorllmentData而不是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());
}
它向我显示一个DisplayMember错误,请你帮我修复它,因为StudentScore类没有StudentID变量所以如何显示StudentScore Class的StudentID?
答案 0 :(得分:1)
您的StudentScore
类型不会公开StudentID
或CourseID
,但这就是您要绑定的内容。你希望它如何工作?
您可以能够使用:
comboBox3.DisplayMember = "EnrollmentData.StudentData.StudentID";
comboBox3.ValueMember = "EnrollmentData.CourseData.CourseID";
...这取决于你使用的绑定是否能够以这种方式导航属性。
顺便说一句,您对StudentTable
和CourseTable
类型的命名是不合适的 - 其实例并不代表StudentTable
- 它代表一行在一张桌子里。只是调用这些类型Student
和Course
会更有意义。
答案 1 :(得分:0)
所以,首先你通过向你展示如何使用点符号来解决Jon解决的无法绑定到新的显示成员错误。
之后,您现在收到无法投射错误。没有看到GetAllScore方法,我不能确切地说出原因。但是,您可以在代码中自行完成繁重工作,并通过向StudentScore添加只读属性来简化绑定,以返回要用作显示和值成员的内容。 (我不得不说使用StudentID作为DisplayMember和CourseID作为ValueMember对我没有任何意义。)
在StudentScore中添加以下内容:
public String UseAsDisplayMember
{
get
{
return EnrollmentData.StudentData.StudentID;
}
}
public string UseAsValueMember
{
get
{
return EnrollmentData.CourseData.CourseID;
}
}
然后将数据绑定设置为:
comboBox3.DisplayMember = "UseAsDisplayMember";
comboBox3.ValueMember = "UseAsValueMember";
这应该让你工作,但感觉更像是一个创可贴,而不是真正的修复。