访问类列表中的字符串

时间:2013-11-26 17:33:19

标签: c# list class listbox

我有一个名为“顾问”的班级,其中包含一个名为“学生建议”的学生名单。我想要做的是在列表框中显示建议一个顾问类实例的学生列表。基本上我想隔离字符串'FirstName'和'LastName',它们是'StudentsAdvised'列表中'Student'类的组成部分。

我尝试使用.datasource填充列表框,但它只显示命名空间的名称。

这就是我所拥有的:

private void adviShowInfoButton_Click(object sender, EventArgs e)
    {
        int index = adviListBox.SelectedIndex;

        advInfoDisplayFirst.Text = AdviserList[index].FirstName;
        advInfoDisplayLast.Text = AdviserList[index].LastName;
        advInfoDisplayDep.Text = AdviserList[index].Department;

        advInfoStudentBox.DataSource = AdviserList[index].StudentsAdvised;


    }

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

private void adviShowInfoButton_Click(object sender, EventArgs e)
{
    int index = adviListBox.SelectedIndex;

    advInfoDisplayFirst.Text = AdviserList[index].FirstName;
    advInfoDisplayLast.Text = AdviserList[index].LastName;
    advInfoDisplayDep.Text = AdviserList[index].Department;

    //this will traverse your StudentsAdvised list
    foreach (Advisers a in AdviserList[index].StudentsAdvised)
    { 
         advInfoStudentBox.Items.AddItem(a.LastName + ", " + a.FirstName);
    }

}