2012-06-28 53 views
-4
DataClasses1DataContext MyCountry = new DataClasses1DataContext("Data source=.; 
User ID=sa; Password=; Integrated security=true; Initial catalog=training;"); 
var _MyCountry = from cntry in MyCountry.GetTable<country>() select cntry; 
grd_table.DataSource = _MyCountry; 

當我嘗試綁定grd_table中的國家時。它顯示錯誤:DataGrid中的問題

System.Windows.Controls.DataGrid' does not contain a definition for 'DataSource' 
and no extension method 'DataSource' accepting a first argument of 
type 'System.Windows.Controls.DataGrid' could be 
found (are you missing a using directive or an assembly reference?)"**. 

我應該怎麼做才能解決這個錯誤。

+4

您是否真的不在乎在MSDN/docs中查找數據網格的屬性? – basti

回答

0

肯定是編譯時錯誤;所以我認爲在構建項目時會拋出錯誤。

它不具有DataSource屬性,請使用ItemsSource代替

0

你是不是應該直接通過數據源,屬性數據表的連接到DataGrid像的WinForms。使用DataBinding! :)

您可以在代碼隱藏設置的數據綁定在XAML

<DataGrid ItemsSource="{Binding Customers}" /> 

或C# - 即使我喜歡XAML路。

dataGrid1.ItemsSource = Customer.GetSampleCustomerList(); 

你真的應該在WPF閱讀MVVMDataBindingCommands,因爲這些技術將讓您在未來的發展有很大幫助。在開始的時候,這比我想象的要容易得多,而且在你學會了第一步之後就很快。

對於我曾經發現的MSDNBlog有datagrid-databinding有一個很好的教程。