Xceed DataGrid for WPF如何绑定主从数据表

作者:控件中国网   出处:控件中国网   2015-09-22 08:56:10   阅读:33

Xceed DataGrid for WPF是一款经典的数据表格控件,支持多种表格常用功能:设计时设计表格、内嵌多种编辑器、数据分组、数据过滤、输入验证、打印和导出等,仅使用少量代码即可实现强大的功能。在表格使用中经常使用到的是主从表,多层显示数据,这篇文章主要介绍如何绑定主从表数据源到Xceed DataGrid for WPF,具体可以参考下面的代码:

C#

static App()

{

 DataSet dataSet = new DataSet();

 string mdbFile = @"Data\Northwind.mdb";

 string connString = String.Format( "Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", mdbFile );

 OleDbConnection conn = new OleDbConnection( connString );

 m_adapter = new OleDbDataAdapter();

 m_adapter.SelectCommand = new OleDbCommand( "SELECT * FROM Employees;", conn );

 m_adapter.Fill( dataSet, "Employees" );

 m_employees = dataSet.Tables[ "Employees" ];    

  m_adapter = new OleDbDataAdapter();

  m_adapter.SelectCommand = new OleDbCommand( "SELECT * FROM Orders;", conn );

  m_adapter.Fill( dataSet, "Orders" );

  m_orders = dataSet.Tables[ "Orders" ];

 

  m_adapter = new OleDbDataAdapter();

  m_adapter.SelectCommand = new OleDbCommand( "SELECT * FROM [Order Details];", conn );

  m_adapter.Fill( dataSet, "Order Details" );

  m_orderDetails = dataSet.Tables[ "Order Details" ];

 m_employees.ChildRelations.Add( new DataRelation( "Employee_Orders", m_employees.Columns[ "EmployeeID" ], m_orders.Columns[ "EmployeeID" ] ) );

 m_orders.ChildRelations.Add( new DataRelation( "Order_OrderDetails", m_orders.Columns[ "OrderID" ], m_orderDetails.Columns[ "OrderID" ] ) );  

}

public static DataTable Employees

{

 get

 {

   return m_employees;

  }

}

private static DataTable m_employees;

private static DataTable m_orders;

private static DataTable m_orderDetails;

private static OleDbDataAdapter m_adapter = null;

 

XAML

<Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"

      xmlns:local="clr-namespace:Xceed.Wpf.Documentation">

 <Grid.Resources>

    <xcdg:DataGridCollectionViewSource x:Key="cvs_employees"

                                       Source="{Binding Source={x:Static Application.Current},

                                                        Path=Employees}"/>   

     <xcdg:IndexToOddConverter x:Key="rowIndexConverter" />

  

     <Style x:Key="alternatingDataRowStyle" TargetType="{x:Type xcdg:DataRow}">

        <Style.Triggers>

           <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},

                                  Path=(xcdg:DataGridVirtualizingPanel.ItemIndex),

                                  Converter={StaticResource rowIndexConverter}}"

                        Value="True">

              <Setter Property="Background" Value="AliceBlue"/>                   

           </DataTrigger>

        </Style.Triggers>

     </Style>

  </Grid.Resources>

 <xcdg:DataGridControl x:Name="EmployeesGrid"

                       ItemsSource="{Binding Source={StaticResource cvs_employees}}"

                       AutoCreateDetailConfigurations="True">  

    <xcdg:DataGridControl.Columns>

       <xcdg:Column FieldName="Photo"

                    Visible="False" />

    </xcdg:DataGridControl.Columns>

    <xcdg:DataGridControl.DetailConfigurations>

       <xcdg:DetailConfiguration RelationName="Employee_Orders"

                                 Title="Employee Orders"

                                 ItemContainerStyle="{StaticResource alternatingDataRowStyle}">

          <xcdg:DetailConfiguration.Columns>

             <xcdg:Column FieldName="EmployeeID"

                          Visible="False" />

          </xcdg:DetailConfiguration.Columns>

          <xcdg:DetailConfiguration.DetailConfigurations>

             <xcdg:DetailConfiguration RelationName="Order_OrderDetails"

                                       Title="Order Details"/>                   

          </xcdg:DetailConfiguration.DetailConfigurations>

      </xcdg:DetailConfiguration>

    </xcdg:DataGridControl.DetailConfigurations>       

 </xcdg:DataGridControl>

</Grid> 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat