0
我看到了一些與我的問題有關的答案。我試過這個答案,但我仍然無法解決這個問題。將數據表添加到WPF中的DataGrid
private void btn_View_Click(object sender, RoutedEventArgs e)
{
try
{
DataTable objDataTable = this.FillDepartment();
this.dataGrid1.ItemsSource = objDataTable.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private DataTable FillDepartment()
{
try
{
Departments objDepartments = new DepartmentServices().GetAllDept();
DataTable objDataTable = new DataTable();
objDataTable.Columns.Add("Department No");
objDataTable.Columns.Add("Department Name");
objDataTable.Columns.Add("Location");
foreach (Department department in objDepartments)
{
DataRow objDataRow = objDataTable.NewRow();
objDataRow[0] = department.DepNo;
objDataRow[1] = department.Name;
objDataRow[2] = department.Loc;
objDataTable.Rows.Add(objDataRow);
}
return objDataTable;
}
catch (Exception)
{
throw;
}
}
使用斷點並通過代碼我看到objDataTable對象返回帶有插入到數據庫中的值的對象。但我不能將這些行添加到DataGrid。希望你們能幫我解決這個問題。
好的,謝謝ü。我會試試這個。 – Shashika 2011-12-17 13:30:48