【问题标题】:Accessing a string within a list of classes访问类列表中的字符串
【发布时间】:2013-11-26 17:33:19
【问题描述】:

我有一个名为“Advisers”的课程,其中包含一个名为“Students Advised”的学生名单。我想要做的是在列表框中显示建议的顾问课程的一个实例的学生列表。基本上我想隔离字符串'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;


    }

【问题讨论】:

    标签: c# list class listbox


    【解决方案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);
        }
    
    }
    

    【讨论】:

    • 没问题,希望一切顺利!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多