2012-03-06 47 views
0

我是新來的WPF和實體框架。在我的應用程序中,我想要做一些非常簡單的事情,將下面的linq結果顯示在數據網格中查詢結果。 這裏是我的查詢綁定數據網格控件到Linq到實體查詢結果

C#

private void applyButton_Click(object sender, RoutedEventArgs e) 
    { 
     string classFilter = classNameTextBox.Text; 
     var result = from c in partNumberContext.PartClasses 
        where c.chrPCName == classFilter       
        select c;    

    } 

這裏是我的XML

<Window x:Class="Engenious.PartNumbers.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Part Details" Height="377" Width="607" Loaded="Window_Loaded"> 
<Window.Resources> 
    <CollectionViewSource x:Key="PartClassSource" /> 
</Window.Resources> 

<Grid Height="346" Width="590"> 

    <Label Content="Class Filter" Height="27" HorizontalAlignment="Left" Margin="75,26,0,0" Name="label1" VerticalAlignment="Top" Width="75" /> 
    <TextBox Height="23" HorizontalAlignment="Right" Margin="0,28,141,0" Name="classNameTextBox" VerticalAlignment="Top" Width="194" /> 
    <Button Content="Apply" Height="23" HorizontalAlignment="Right" Margin="0,27,26,0" Name="applyButton" VerticalAlignment="Top" Width="75" Click="applyButton_Click" /> 
    <DataGrid AutoGenerateColumns="False" Height="263" HorizontalAlignment="Left" Margin="53,71,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" /> 
</Grid> 

+0

解決它。只需設置dataGrid1.AutoGenerateColumns = true; – IamaC 2012-03-06 19:24:05

回答

0

設置在數據網格中的AutoGenerateColumns爲true。

下面是代碼:

<Window x:Class="Engenious.PartNumbers.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Part Details" Height="377" Width="607" Loaded="Window_Loaded"> 
<Window.Resources> 
    <CollectionViewSource x:Key="PartClassSource" /> 
</Window.Resources> 

<Grid Height="346" Width="590"> 

    <Label Content="Class Filter" Height="27" HorizontalAlignment="Left" Margin="75,26,0,0" Name="label1" VerticalAlignment="Top" Width="75" /> 
    <TextBox Height="23" HorizontalAlignment="Right" Margin="0,28,141,0" Name="classNameTextBox" VerticalAlignment="Top" Width="194" /> 
    <Button Content="Apply" Height="23" HorizontalAlignment="Right" Margin="0,27,26,0" Name="applyButton" VerticalAlignment="Top" Width="75" Click="applyButton_Click" /> 
    <DataGrid AutoGenerateColumns="true" Height="263" HorizontalAlignment="Left" Margin="53,71,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" /> 
</Grid>