Question

What code can I add to make datagrid display not all columns(e.g. 2 of all) of returned dataset DefaultView using objectdataprovider? I used this tutorial to write this working code.

Here are woking XAML code:

<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>

SOLUTION

<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>
Was it helpful?

Solution

Change Autogenerate columns to false in Datagrid.

If Dataset returns 3 columns Id, Name and Address and you want to skip the Name Then Add those columns in DataGrid.Columns and provide binding path.

Try this:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top