2016-05-15 37 views
1

我試圖顯示使用WPF數據網格這可是沒有數據源像的WinForms DataGridView中我LINQ結果..WPF LINQ的結果數據網格

我一直是這樣,現在掙扎小時,只是不明白我需要做什麼。我的代碼如下:

private void BindDataGrid(string searchString) //passed on button click 
    { 
     DatabaseEntities entity = new DatabaseEntities(); 

      var postcodes = (from p in entity.postcode_table 
          where p.postcode.Equals(searchString) 
          orderby p.postcode ascending 
          select p); 
      dataGrid.ItemsSource = postcodes; 
    } 

這裏的任何幫助將是偉大的。這不是我每天做的事情,所以請耐心等待。

謝謝

+1

你對你的代碼有任何問題嗎?你能否更好地解釋你需要做什麼? – ASh

+0

我的代碼沒有問題。我用文本框和按鈕創建了一個wpf表單。當輸入郵編並單擊按鈕時,我需要從數據庫中檢索輸入的郵編的詳細信息。 – Pete

回答

0

誰告訴DataGrid沒有源代碼?見下面的代碼。

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

這裏MyDataGridSource是的ObservableCollection其中包含的數據。填充它,它應該工作。

+0

不幸的是,這並沒有奏效。按下按鈕時,網格會填充兩個空白行。 – Pete