Share via


チュートリアル: DataGrid コントロールで SQL Server データベースのデータを表示する

このチュートリアルでは、SQL Server データベースからデータを取得して、DataGrid コントロールでそのデータを表示します。 ADO.NET Entity Framework を使用して、データを表すエンティティ クラスを作成し、LINQ を使用して、エンティティ クラスから指定したデータを取得するためのクエリを記述します。

必須コンポーネント

このチュートリアルを実行するには、次のコンポーネントが必要です。

  • Visual Studio 2010.

  • AdventureWorksLT2008 サンプル データベースがアタッチされた、SQL Server または SQL Server Express の実行中のインスタンスへのアクセス。 AdventureWorksLT2008 データベースは、CodePlex の Web サイトからダウンロードできます。

エンティティ クラスを作成するには

  1. Visual Basic または C# で新しい WPF アプリケーション プロジェクトを作成して、「DataGridSQLExample」という名前を付けます。

  2. ソリューション エクスプローラーでプロジェクトを右クリックし、[追加] をポイントして、[新しいアイテム] をクリックします。

    [新しい項目の追加] ダイアログ ボックスが表示されます。

  3. [インストールされたテンプレート] ペインで [データ] を選択し、テンプレートの一覧で [ADO.NET エンティティ データ モデル] を選択します。

    ADO.NET エンティティ データ モデルの選択

  4. ファイルに「AdventureWorksModel.edmx」という名前を付け、[追加] をクリックします。

    Entity Data Model ウィザードが表示されます。

  5. [モデルのコンテンツの選択] 画面で、[データベースから生成] をクリックし、[次へ] をクリックします。

    [データベースから生成] オプションの選択

  6. [データ接続の選択] 画面で、AdventureWorksLT2008 データベースへの接続情報を入力します。 詳細については、「[データ接続の選択] ダイアログ ボックス」を参照してください。

    データベースへの接続の提供

  7. 名前が「AdventureWorksLT2008Entities」であり、[エンティティ接続設定に名前を付けて App.Config に保存] チェック ボックスがオンになっていることを確認し、[次へ] をクリックします。

  8. [データベース オブジェクトの選択] 画面で、[テーブル] ノードを展開し、Product テーブルと ProductCategory テーブルをクリックします。

    エンティティ クラスはすべてのテーブルについて生成できますが、この例ではこれら 2 つのテーブルのデータのみを取得します。

    テーブルからの Product および ProductCategory の選択

  9. [完了] をクリックします。

    Product エンティティと ProductCategory エンティティが Entity Designer に表示されます。

    Product および ProductCategory エンティティ モデル

データを取得して表示するには

  1. MainWindow.xaml ファイルを開きます。

  2. Window 上の Width プロパティを「450」に設定します。

  3. XAML エディターで、<Grid> タグと </Grid> タグの間に次の DataGrid タグを追加して、dataGrid1 という名前の DataGrid を追加します。

    <DataGrid Name="dataGrid1" />
    

    DataGrid のあるウィンドウ

  4. Window を選択します。

  5. プロパティ ウィンドウまたは XAML エディターを使用して、Loaded イベントに対し、Window のイベント ハンドラーを Window_Loaded という名前で作成します。 詳細については、「方法 : 単純なイベント ハンドラーを作成する」を参照してください。

    MainWindow.xaml の XAML を次に示します。

    メモメモ

    Visual Basic を使用している場合は、MainWindow.xaml の最初の行で、x:Class="DataGridSQLExample.MainWindow" を x:Class="MainWindow" で置換します。

    <Window x:Class="DataGridSQLExample.MainWindow"
            xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="450" 
            Loaded="Window_Loaded">
        <Grid>
            <DataGrid Name="dataGrid1" />
        </Grid>
    </Window>
    
  6. Window の分離コード ファイル (MainWindow.xaml.vb または MainWindow.xaml.cs) を開きます。

  7. 次のコードを追加して、結合されたテーブルの特定の値のみを取得し、DataGridItemsSource プロパティを、クエリの結果に設定します。

    Imports System.Data.Objects
    
    Class MainWindow
        Dim dataEntities As AdventureWorksLT2008Entities = New AdventureWorksLT2008Entities
    
        Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
            Dim products As ObjectQuery(Of Product) = dataEntities.Products
    
            Dim query = _
                From product In products _
                Where product.Color = "Red" _
                Order By product.ListPrice _
                Select product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice
    
            dataGrid1.ItemsSource = query.ToList()
        End Sub
    End Class
    
    using System.Data.Objects;
    using System.Linq;
    using System.Windows;
    
    namespace DataGridSQLExample
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            AdventureWorksLT2008Entities dataEntities = new AdventureWorksLT2008Entities();
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                ObjectQuery<Product> products = dataEntities.Products;
    
                var query =
                from product in products
                where product.Color == "Red"
                orderby product.ListPrice
                select new { product.Name, product.Color, CategoryName = product.ProductCategory.Name, product.ListPrice };
    
                dataGrid1.ItemsSource = query.ToList();
            }
        }
    }
    
  8. 例を実行します。

    DataGrid にデータが表示されることを確認します。

    SQL データベースからのデータがある DataGrid

参照

参照

DataGrid

その他の技術情報

How Do I: Get Started with Entity Framework in WPF Applications? (操作方法: WPF アプリケーションで Entity Framework を使用する)