2016-05-29 63 views
0

的ITs XAML代碼爲DataGrid的無法轉換爲DataRowView的WPF中

<DataGrid AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="10,30,0,0" ItemsSource="{Binding}" VerticalAlignment="Top" Height="149" Width="356" Name="orderGrid" SelectionMode="Single" SelectionUnit="FullRow" SelectionChanged="orderGrid_SelectionChanged" /> 

這裏是我的查詢和項目來源

var orders = (from ss in db.electronics_orders 
         select new { ss.id,Date = ss.orderdate,Product_Title=ss.Electronic.Name, Quantity = ss.qty, TPrice = ss.qty * ss.unitprice}).ToList(); 
     orderGrid.ItemsSource = orders; 

我現在想投選擇項目DataRowView的

try 
      { 
       DataRowView grid = (DataRowView)orderGrid.CurrentItem; 
      } 
      catch (Exception e1) { 
       MessageBox.Show(e1.Message); 
      } 

但它顯示錯誤

無法轉換 類型的對象 '<> f__AnonymousType0 5[System.Int32,System.Nullable 1的System.DateTime],System.String,System.Nullable 1[System.Int32],System.Nullable 1 [System.Int32]]' 爲類型 '的System.Data.DataRow'。

回答

0

當你在LINQ(select new {ss.id,...})選擇,你基本上得到一個新的內部匿名類,這是不是一個DataRow,喜歡它的期望。 您必須轉換它,或者使用綁定的對象類型,以便它們「兼容」。

相關問題