2011-03-04 126 views
1

我正在從Windows窗體移動到WPF,但現在我有一個問題。wpf:如何在網格視圖中顯示數據集?

我從數據庫(SQL Server)的信息,並存儲在數據集中,我想表明,在一個DataGrid(DG)

DataSet ds = new DataSet(); 
SqlConnection sc = new SqlConnection("mysqlconnection"); 
SqlDataAdapter sd = new SqlDataAdapter(); 
sc.Open(); 
sd.SelectCommand = new SqlCommand("SELECT * FROM table_1", sc); 
sd.Fill(ds); 
dg.DataContext = ds.Tables[0].DefaultView;//here is the problem 
sc.Close(); 

在Windows窗體是dg.DataSrouce,但我無法找到在wpf中,有什麼幫助?

回答

3

要麼添加ItemsSource="{Binding}"DataGrid定義或改變

dg.DataContext = ds.Tables[0].DefaultView; 

dg.ItemsSource = ds.Tables[0].DefaultView; 

更新
嘗試添加AutoGenerateColumns="True"

<DataGrid Name="dg" 
      AutoGenerateColumns="True" 
      ItemsSource="{Binding}" 
      ...> 
+0

感謝您的回答,我可以看到有4行是真的,但我沒有看到這4行內的任何列或數據! – trrrrrrm 2011-03-04 07:42:08

+0

@ From.ME.to.YOU:更新了我的答案,嘗試在'DataGrid'定義中設置'AutoGenerateColumns ='True'' – 2011-03-04 07:43:55

+0

非常感謝,像魔術一樣工作! – trrrrrrm 2011-03-04 07:48:14

相關問題