[英]How to bind list of objects to datagrid?
首先,我阅读了所有微笑问题,并尝试了答案。 似乎对我没有任何帮助。
我创建了一个名为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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.