如何将对象列表绑定到数据网格?

3

首先,我已经阅读了所有类似的问题并尝试了它们的答案。但对我来说都没有用。

我创建了一个名为student的类。然后从数据库中加载了一组学生。

现在,我希望在数据网格中显示这个列表。但是我只得到了一个空表.. :(

问题出在哪里呢?(我尝试了使用datacontext而不是itemSource)

我的C#代码:

public partial class MainWindow : Window
{
    public List<student> studentsList = new List<student>();
    public MainWindow()
    {
        try
        {
            InitializeComponent();
            Classes.studentManager stdManager = new Classes.studentManager();


            studentsList = stdManager.Select();

            this.dgStudents.DataContext = studentsList.ToList();

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

以下是XAML代码:
 <DataGrid AutoGenerateColumns="False" Height="211" Name="dgStudents" Width="Auto" Margin="39,0,-33,-142" VerticalAlignment="Bottom" ItemsSource="{Binding}">
   <DataGrid.Columns>
     <DataGridTextColumn Header="birthDate" Width="175" Binding="{BindingfirstName}" />
    </DataGrid.Columns>
   </DataGrid>

这是一个学生对象

  public class student
  {

    public int ID;
    public string firstName;
    public string lastName;
    public string studentID;
    public string homePhone;
    public string cellPhone;
    public string parentsPhone;
    public string parentsName;
    public string adress;
    public bool isPicFormExists;
    public string medProblems;
    public bool isParentsConfExists;
    public List<Class> classesList;
    public DateTime birthDate;
    public List<payments> payments;
    public double accountBalance;


    public student() 
    {
        try
        {

        }
        catch (Exception ex)
        {
            //TO-DO
        }
    }

}

你的学生类中有属性或变量吗? - Nitin
这个语句的作用是什么:“studentsList = stdManager.Select();”? - Naren
感谢您的回复。Andrey已经解决了它。正如nit建议的那样,我将其更改为属性... - Shaul Zuarets
1个回答

5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接