【问题标题】:How to make datagrid display not all columns of returned dataset using objectdataprovider?如何使用 objectdataprovider 使 datagrid 不显示返回数据集的所有列?
【发布时间】:2013-10-06 05:43:38
【问题描述】:

我可以添加什么代码以使数据网格不显示使用 objectdataprovider 返回的数据集 DefaultView 的所有列(例如全部 2 个)?我用这个tutorial 编写了这个工作代码。

这里是工作 XAML 代码:

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

【问题讨论】:

    标签: c# wpf visual-studio datagrid objectdataprovider


    【解决方案1】:

    在 Datagrid 中将自动生成列更改为 false。

    如果数据集返回 3 列 ID、名称和地址,并且您想跳过名称,则在 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>
    

    【讨论】:

    • Message=在使用 ItemsSource 时,操作不可用。在 ItemsControl.ItemsSource 的帮助下访问和更改元素。 Source=PresentationFramework(翻译后的缓冲异常解释)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2016-06-08
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多