NetAdvantage WPF Express - xamDataGrid

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

エッセンシャルWPF:Windows Presentation Foundation (Programmer's SELECTION)

NetAdvantage WPF Express - xamDataGrid

MSから提供されているデータグリッドは、機能がまだまだ足りないためinfragistics社のデータグリッドを試す。
infragisticsのサイトから「NetAdvantage for WPF Express 2009 Volume 1」をダウンロード、インストール

セルの値を取得、追加、削除

Windows1.xaml
<Window x:Class="WpfInfragisticsSample.Window1"
    xmlns="htt[asin:4798114200:detail]
p://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="0.7*"></RowDefinition>
            <RowDefinition Height="0.3*"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
        </Grid.RowDefinitions>
        
        <igDP:XamDataGrid 
            Name="xamDataGrid1" 
            HorizontalAlignment="Stretch"  
            VerticalAlignment="Stretch" 
            GroupByAreaLocation="None"
            RecordActivated="xamDataGrid1_RecordActivated"
            CellActivated="xamDataGrid1_CellActivated"
            AutoFit="True"
            xmlns:igDP="http://infragistics.com/DataPresenter">
            <igDP:XamDataGrid.FieldSettings >
                <igDP:FieldSettings AllowEdit="True" />
                
            </igDP:XamDataGrid.FieldSettings> 
            <igDP:XamDataGrid.FieldLayoutSettings>
                <igDP:FieldLayoutSettings 
                    AllowDelete="True" 
                    AllowAddNew="True" 
                    
                    SelectionTypeRecord="Single"
 		            AddNewRecordLocation="OnBottom"                     
                    />
            </igDP:XamDataGrid.FieldLayoutSettings>
        </igDP:XamDataGrid>
        
        <StackPanel Orientation="Vertical" Grid.Row="1">
            <TextBox Height="24" Width="200" Name="textBox1" />
            <TextBox Height="24" Width="200" Name="textBox2" />
            <TextBox Height="24" Width="200" Name="textBox3" />
        </StackPanel>
        <Button Grid.Row="2" HorizontalAlignment="Left" Name="button2" Width="108" Click="button2_Click" Margin="0,3.5">DataTable内容表示</Button>
        <Button Name="button1" Height="23" Width="50" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center" Click="button1_Click">取得</Button>
        <Button Name="button3" Height="23" Width="50" Grid.Row="2" HorizontalAlignment="Right" VerticalAlignment="Center" Click="button3_Click">表示</Button>
    </Grid>
</Window>
Windows1.xaml.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Infragistics.Windows.DataPresenter.Events;
using Infragistics.Windows.DataPresenter;

namespace WpfInfragisticsSample
{
    /// <summary>
    /// Window1.xaml の相互作用ロジック
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private DataTable dtUser;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this.dtUser = new DataTable();
            this.dtUser.Columns.Add("ID");
            this.dtUser.Columns.Add("NAME");
            this.dtUser.Columns.Add("BIRTHDAY");
            this.dtUser.Rows.Add("1", "ken sasaki", "2009/6/12");
            this.dtUser.Rows.Add("2", "izumi nakata", "2009/6/13");
            this.dtUser.Rows.Add("3", "sino takagi", "2009/6/14");
            this.xamDataGrid1.DataSource = this.dtUser.DefaultView;
        }
        
        private void xamDataGrid1_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {
            //セルの値を取得するサンプル
            if (e.Record is DataRecord)
            {
                DataRecord myRecord = (DataRecord)e.Record;
                if (myRecord.DataItem != null)
                {
                    this.textBox1.Text = myRecord.Cells["ID"].Value.ToString();
                    this.textBox2.Text = myRecord.Cells["NAME"].Value.ToString();
                    this.textBox3.Text = myRecord.Cells["BIRTHDAY"].Value.ToString();
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string data = string.Empty;
            
            foreach (DataRow dr in dtUser.Rows)
            {
                data += dr[0].ToString() + dr[1].ToString() + dr[2].ToString() + Environment.NewLine;
            }

            MessageBox.Show(data);
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            Window2 w2 = new Window2();
            w2.textBox1.Text = this.textBox1.Text;
            w2.textBox2.Text = this.textBox2.Text;
            w2.textBox3.Text = this.textBox3.Text;
            w2.ShowDialog();
            
            DataRecord record = this.xamDataGrid1.ActiveRecord as DataRecord;

            if (record.DataItem != null)
            {
                record.Cells[0].Value = w2.textBox1.Text;
                record.Cells[1].Value = w2.textBox2.Text;
                record.Cells[2].Value = w2.textBox3.Text;
            }
        }

        private void xamDataGrid1_CellActivated(object sender, CellActivatedEventArgs e)
        {
            Cell cell = e.Cell;
            MessageBox.Show(cell.Value.ToString());
        }
    }
}

セルの値を編集不可とする

Window1.xaml.cs
    <igDP:XamDataGrid 
        Name="xamDataGrid1" 
        HorizontalAlignment="Stretch"  
        VerticalAlignment="Stretch" 
        GroupByAreaLocation="None"
        RecordActivated="xamDataGrid1_RecordActivated"
        AutoFit="True"
        xmlns:igDP="http://infragistics.com/DataPresenter">
        <igDP:XamDataGrid.FieldSettings >
            <igDP:FieldSettings AllowEdit="False" />
        </igDP:XamDataGrid.FieldSettings> 
    </igDP:XamDataGrid>

選択するレコードを1行にする

Window1.xaml
    <igDP:XamDataGrid 
        Name="xamDataGrid1" 
        HorizontalAlignment="Stretch"  
        VerticalAlignment="Stretch" 
        GroupByAreaLocation="None"
        RecordActivated="xamDataGrid1_RecordActivated"
        CellActivated="xamDataGrid1_CellActivated"
        AutoFit="True"
        xmlns:igDP="http://infragistics.com/DataPresenter">
        <igDP:XamDataGrid.FieldSettings >
            <igDP:FieldSettings AllowEdit="True" />
        </igDP:XamDataGrid.FieldSettings> 
        <igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:FieldLayoutSettings 
                AllowDelete="True" 
                AllowAddNew="True" 
                SelectionTypeRecord="Single"
                AddNewRecordLocation="OnBottom"
                />
        </igDP:XamDataGrid.FieldLayoutSettings>
    </igDP:XamDataGrid>