SQLの窓

2013年10月18日


WPF(C#) : ExecuteQuery<T> を使って、DataGrid に自動的に select 構文の結果を表示する



※ System.Data.Linq を参照して下さい

元々は、Windows Phone のテンプレートにあるバインド方法ですが、DataGrid にある AutoGenerateColumns プロパティはデフォルトで ON です。そもそも、こういう目的に使えるようにできています。

テーブル定義用のクラスは、ObservableCollection<T> でまとめられる必要があるので、ExecuteQuery の結果を使って ObservableCollection<T> を作り直して DataContext に設定します。その際、ItemsSource は、{Binding} である必要があります。

※ 社員マスタの列はもっとたくさんあります

MainWindow.xaml
<Window x:Class="WpfDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
		<DataGrid
			Height="217"
			HorizontalAlignment="Left"
			Margin="54,56,0,0"
			Name="dataGrid1"
			VerticalAlignment="Top"
			Width="399"
			AutoGenerateColumns="True"
			ItemsSource="{Binding}" />

		<Button
			Content="Button"
			Height="24"
			HorizontalAlignment="Left"
			Margin="55,17,0,0"
			Name="button1"
			VerticalAlignment="Top"
			Width="113"
			Click="button1_Click" />
	</Grid>
</Window>


MainWindow.xaml.cs
using System;
using System.Collections.Generic;
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 System.Data;
using System.Data.Odbc;
using System.Diagnostics;
using System.Data.Linq;
using System.Collections.ObjectModel;

namespace WpfDataGrid
{

    public partial class MainWindow : Window
    {
        private OdbcConnection cn = null;

        public MainWindow()
        {
            InitializeComponent();
        }

        private class Syain
        {
            public string @社員コード { get; set; }
            public string @氏名 { get; set; }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            string cs = "Driver={MySQL ODBC 5.2 Unicode Driver};" +
			            "Server=localhost;" +
			            "Database=lightbox;" +
			            "Uid=root;" + 
			            "Pwd=password;";

            string query = String.Format("select 社員コード,氏名 from 社員マスタ where 社員コード >= '{0}'", "0005");

            try {
                cn = new OdbcConnection(cs);
                DataContext context = new DataContext(cn);
                this.dataGrid1.DataContext = 
                    new ObservableCollection<Syain>(
                        context.ExecuteQuery<Syain>(query)
                    );
            }
            catch( Exception ex ) {
                Debug.WriteLine(ex.Message);
            }

            if ( cn.State == ConnectionState.Open ) {
                cn.Close();
                cn.Dispose();
            }

        }
    }
}

ただ、VS2010 では、バグがあるようで、行が1行多く作成されてしまいます。VS2012 ではうまく行くんですが・・・。それと、ExecuteQuery の変数バインド機能は動作しませんでした。

CanUserAddRowsを 意図的に false にする必要があります。( デフォルトが true でした )。後、セル内の編集が必要無いのであれば、IsReadOnly を true にしてもかまいません。


【VS(C#)の最新記事】
posted by lightbox at 2013-10-18 03:04 | VS(C#) | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり