オブジェクトを Windows フォーム コントロールにバインドする方法 (Entity Framework)
Entity Framework を使用すると、EntityCollection や ObjectQuery の結果に対して ComboBox や DataGridView などの Windows フォーム コントロールをバインドできます。 コントロールを ObjectQuery に直接バインドしないことをお勧めします。 代わりに、コントロールを Execute メソッドの結果にバインドします。 詳細については、「コントロールへのオブジェクトのバインド (Entity Framework)」を参照してください。
このトピックの例には、Adventure Works Sales Model が使用されています。 この例のコードを実行するには、あらかじめプロジェクトに AdventureWorks Sales Model を追加し、Entity Framework が使用されるようにプロジェクトを構成しておく必要があります。 具体的な方法については、「Entity Framework プロジェクトを手動で構成する方法」および「方法: モデル ファイルとマッピング ファイルを手動で定義する (Entity Framework)」の手順を参照してください。
例
次の例は、Windows フォームからの抜粋です。 フォームの読み込み時に、ObjectQuery の Execute メソッドを呼び出して、SalesOrderHeader オブジェクトの ObjectResult が返されます。 この結果は、コンボ ボックスにバインドされます。 注文が選択されると、SalesOrderDetail オブジェクトの関連する EntityCollection が DataGridView コントロールにバインドされます。
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
namespace Microsoft.Samples.Edm
{
public partial class Main : Form
{
private AdventureWorksEntities context;
private int customerId = 277;
public Main()
{
// Initializes the designer-generated controls.
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
// Initialize the object context.
context = new AdventureWorksEntities();
try
{
// Create a query for orders that includes line items.
ObjectQuery<SalesOrderHeader> orderQuery = context.SalesOrderHeaders
.Where("it.CustomerID = @customerId",
new ObjectParameter("customerId", customerId))
.Include("SalesOrderDetails");
// Display the PO number in the combo box.
this.ordersListBox.DisplayMember = "PurchaseOrderNumber";
// Bind the combo box to the ObjectResult of SalesOrderHeader
// that is returned when the query is executed.
this.ordersListBox.DataSource = orderQuery.Execute(MergeOption.AppendOnly);
}
catch (EntitySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
private void ordersListBox_SelectedIndexChanged(object sender, EventArgs e)
{
// Get the currently selected SalesOrderHeader object.
SalesOrderHeader order = (SalesOrderHeader)this.ordersListBox.SelectedItem;
// Bind the items for this order to the DataGridView.
lineItemsDataGrid.DataSource = order.SalesOrderDetails;
}
private void saveButton_Click(object sender, EventArgs e)
{
// Get the current order.
SalesOrderHeader order = (SalesOrderHeader)ordersListBox.SelectedItem;
try
{
// Save changes in the object context.
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrently conflict by refreshing the
// object context before saving changes.
context.Refresh(RefreshMode.ClientWins, order.SalesOrderDetails);
// Resave changes in the object context.
context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.Message, "An error has occured");
}
finally
{
// Refresh the latest data from the database.
context.Refresh(RefreshMode.StoreWins, order);
this.Refresh();
}
}
}
}
参照
処理手順
オブジェクトを Windows Presentation Foundation コントロールにバインドする方法 (Entity Framework)
オブジェクトをプロジェクト データ ソースとして追加する方法 (Entity Framework)