2010-11-09 134 views
4

我想在我的WPF DataGrid中顯示的數據表的內容如何將數據表加載到WPF數據網格

在下面的帖子:

Why can't I bind the WPFToolkit DataGrid ItemSource to DataTable?

我找到的代碼:

myDataGrid.ItemsSource = myDataTable.DefaultView; 

行爲很奇怪:它顯示n個空行,其中n是正確的:查詢結果n行!

那麼,爲什麼我看不到他們呢?

+1

DataGrid是否自動生成列? – 2010-11-09 13:48:30

+0

+1是的我已經找到它... myGrid.AutoGenerateColumns = true;確實是這個問題。 – Asaf 2010-11-09 15:57:18

回答

2

在沒有看到代碼,我猜你沒有AutoGenerateColumns設置爲真。這很好,但如果您不是自動生成列,則需要手動將列定義添加到數據網格。

-2

一種可能的解決方案是將DataGrid替換爲DataGridView,並將DataGridView.DataSource屬性綁定到DataTable本身。按this example

dataGrid1.DataSource = table;

+0

我們在談論WPF。 DataGridView是一個WinForms控件。 – TecMan 2016-09-28 07:44:33

相關問題