2013-09-30 3 views
0

DataGrid가 objectdataprovider를 사용하여 반환 된 데이터 세트 DefaultView의 모든 열 (예 : 2 개)을 표시하지 않도록 추가 할 수있는 코드는 무엇입니까? 이 코드를 쓰려면이 tutorial을 사용했습니다. 여기 objectdataprovider를 사용하여 반환 된 데이터 집합의 모든 열을 DataGrid에 표시하는 방법은 무엇입니까?

는 XAML 코드를 워킹됩니다에서 Datagrid에서 false로

<Window x:Class="wpf_2._0.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:wpf_2._0" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <!-- create an instance of our DataProvider class --> 
     <ObjectDataProvider x:Key="robot_data" 
      ObjectType="{x:Type local:robot_data}"/> 
     <!-- define the method which is invoked to obtain our data --> 
     <ObjectDataProvider x:Key="РОБОТ" 
      ObjectInstance="{StaticResource robot_data}" 
      MethodName="get_robot_data"/> 
    </Window.Resources> 
    <Grid > 
      <DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="True" HorizontalAlignment="Left" Margin="85,127,0,0" VerticalAlignment="Top" Height="133" Width="265" SelectionChanged="DataGrid_SelectionChanged_1"/> 
    </Grid> 
</Window> 

솔루션

<Window x:Class="wpf_2._0.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:wpf_2._0" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <!-- create an instance of our DataProvider class --> 
     <ObjectDataProvider x:Key="robot_data" 
      ObjectType="{x:Type local:robot_data}"/> 
     <!-- define the method which is invoked to obtain our data --> 
     <ObjectDataProvider x:Key="РОБОТ" 
      ObjectInstance="{StaticResource robot_data}" 
      MethodName="get_robot_data"/> 
    </Window.Resources> 
    <Grid > 
     <DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="30,73,0,0" VerticalAlignment="Top" Height="133" Width="97"> 
      <!-- create an instance of our DataProvider class --> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding Path=имя_робота}" Header="Имя робота"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

답변

0

변경 자동 생성 된 열을.

데이터 세트가 ID, 이름 및 주소 3 개의 열을 반환하고 이름을 건너 뛰려면 해당 열을 DataGrid.Columns에 추가하고 바인딩 경로를 제공해야합니다.

이 시도 :

<DataGrid ItemsSource="{Binding Source={StaticResource РОБОТ}}" AutoGenerateColumns="False" HorizontalAlignment="Left" 
    Margin="85,127,0,0" VerticalAlignment="Top" Height="133" Width="265" SelectionChanged="DataGrid_SelectionChanged_1"> 
      <DataGrid.Columns> 
        <DataGridTextColumn Header="id" Binding="{Binding Path=Id}"/> 
        <DataGridTextColumn Header="address" Binding="{Binding Path=Address}"/> 
      </DataGrid.Columns> 
</DataGrid> 
+0

메시지 = 동작을 사용할 수없는, ItemsSource 사용하는 경우. ItemsControl.ItemsSource를 사용하여 요소에 액세스하고 요소를 변경하십시오. 소스 = PresentationFramework (번역 된 버퍼링 된 예외 설명) – NDGO

+0

같은 오류가 발생했습니다. – NDGO