将数据表绑定到xamdatagrid

时间:2016-08-17 01:52:55

标签: binding xamdatagrid

我不知道如何将列添加到xamDatagrid并将datatalbe绑定到此。 有人可以帮帮我吗?

        DataTable dt = new DataTable();
        dt.Columns.Add("Kieu", typeof(int));
        dt.Columns[0].DefaultValue = 1;
        dt.Columns.Add("Ngay", typeof(DateTime));
        dt.Columns[1].DefaultValue = DateTime.Today;
        dt.Rows.Add();

1 个答案:

答案 0 :(得分:0)

你可以这样做:

XAML:

<Window x:Class="WPFXamDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-mpatibility/2006"
        xmlns:igDP="http://infragistics.com/DataPresenter"
        xmlns:local="clr-namespace:WPFXamDataGrid"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <igDP:XamDataGrid x:Name="xamDataGrid" Loaded="XamDataGrid_Loaded">
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:Field Name="Kieu"/>
                    <igDP:Field Name="Ngay"/>
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
    </Grid>
</Window>

代码背后:

using System;
using System.Windows;
using System.Data;

namespace WPFXamDataGrid
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void XamDataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Kieu", typeof(int));
            dt.Columns.Add("Ngay", typeof(DateTime));

            dt.Rows.Add(1, DateTime.Now);

            xamDataGrid.DataSource = dt.DefaultView;
        }
    }
}