方法 : Pocket PC で、データ グリッドを使用します。

[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]

この例ではフォームの表示と、DataGrid コントロールで選択したレコードを編集およびデータベースに新しいレコードを追加 DataGrid コントロールを使用する方法を示します。 DataGrid セルを編集するため、 DataGrid 値を編集するユーザー インターフェイスを提供する必要がありますあるメモは .NET Compact Framework でサポートされていません。 次の使用例は、Visual Studio と一緒にインストールされるノースウィンド データベース] を使用します。

注意

.NET Compact Framework バージョン 2. 0 を使用している場合は DataGrid コントロールを使用してプロジェクトに System.Windows.Forms.DataGrid.dll への参照を追加する必要があります。

BindingSource オブジェクトを現在選択されているは、概要のコンストラクターに渡され、同じバインド ソースのすべてのフォームを使用するようにフォームを編集する、データベース レコードにアクセスできます。 データ連結コントロール、に加えて BindingSource オブジェクトを現在の行の DataRowView オブジェクトを返すことができます。 現在の列の値を決定するなど、目的のさまざまなデータにアクセスする DataRowView を使用できます。 注意デモのために、2 つの列は、集計用の次の使用例で使用してくださいフォームを編集します。

また、Visual Studio サマリーを生成してデータ フォームを生成 を DataGrid コントロールにスマート タグのショートカット メニューから選択するフォームを自動的に編集ができます。 この機能の詳細については、「方法 : (デバイス) のデータ アプリケーションの概要と編集ビューを生成します。」を参照してください。

このアプリケーションが、フォームの次の表にしています。 一覧に表示] メニューの [オプションしています。

Form

機能

メニュー オプション

メイン フォーム

(Form1)

DataGrid コントロールを表示します。

新規

データベースに新しいレコードを追加し、 フォームの EditView を表示します。

編集

フォームの EditView を表示します。

SummaryView

表示用に、カレント レコードの表示列の値最適化されています。

(なし)

EditView

編集用に、カレント レコードの表示列の値最適化されています。

チェック

は指定、ダイアログ ボックスをデータベースを更新でき表示、メイン フォームします。

[キャンセル]

ダイアログ ボックスをキャンセルし、メイン フォームを表示します。

プロジェクトを作成し、メイン フォームをデザインします。

  1. Visual Studio でスマート デバイス プロジェクトを作成し、Windows Mobile 5. 0 の Pocket PC SDK または Windows Mobile 6 Professional SDK に、ターゲット プラットフォームを設定します。

  2. [データ] メニューの [新しいデータ ソースの追加] をクリックします。

  3. データ ソース構成ウィザード] で] Northwind データベースに Microsoft SQL Server Compact (.NET Framework データ プロバイダーの Microsoft SQL Server Compact) を使用して接続します。 Northwind のデータベース、Northwind.sdf は、\Program Files\Microsoft の SQL Server Compact Edition\v3.5\Samples フォルダーにインストールされます。

    注意

    Windows Vista の場合は、[Northwind データベースへのアクセスに管理者として Visual Studio を実行する必要があります。データベースの追加については、方法 : デバイス プロジェクトにデータベースを追加します。 を参照してください。

  4. In the Choose Your Database Objects page of the wizard, select the Products table and all of its columns.

  5. のツールボックス、 DataGrid コントロールをフォームに追加します。 必要に応じてサイズとレイアウト プロパティを設定します。

  6. DataSource プロパティ テーブルに設定します。 Visual Studio をプロジェクトに、 NorthwindDataSetや ProductsBindingSourceProductsTableAdapter オブジェクトを追加します。

  7. スタイル、DataGridDataGridTableStyle オブジェクト TableStyles コレクションに追加して、テーブルから 1 つまたは 2 つの列を表示コントロールです。 プロパティ ペインで TableStyles プロパティをクリックします。 この操作では、DataGridTableStyle コレクション エディター] ダイアログボックスが表示されます。 次の操作を行います。

    1. DataGridTableStyle コレクションに、 TableStyles オブジェクトを追加します。

    2. 「製品」を指定します。 の MappingName プロパティ。

    3. プロパティの GridColumnStyle をクリックします。 この操作では、DataGridColumnStyle コレクション エディター] ダイアログボックスが表示されます。

    4. DataGridTextBoxColumn コレクションに、 GridColumnStyles オブジェクトを追加します。

    5. の MappingName プロパティをクリックし、 の製品名を選択します。

    6. ヘッダー テキストの目的 と幅の を設定します。

    7. 追加の列を繰り返します。

    8. ダイアログ ボックスを閉じます。

  8. プロジェクトのサマリー ビューとエディット ビューの 2 つのフォームを追加します。 SummaryView の および EditView の に名前をします。

  9. SummaryView の と、 オブジェクトに EditView BindingSource のフォームのコンストラクターにパラメーターを追加します。 CurrentBindingSouce、これらのフォームをコンストラクターに渡される、 BindingSource オブジェクトに設定でグローバル変数を宣言します。 なお前 InitializeComponent メソッドに、設定する必要があります"と呼びます。

    コード ペインで右上隅 メソッド名 リストから新しい メソッドを追加して、フォームに Sub New を追加する Visual Basic 開発者必要があります。

    Dim CurrentBindingSource As BindingSource
    Public Sub New(ByVal bsource As BindingSource)
        CurrentBindingSource = bsource
        InitializeComponent()
    End Sub
    
    private BindingSource CurrentBindingSource;
    public SummaryView(BindingSource bsource)
    {
        CurrentBindingSource = bsource;
        InitializeComponent();
    }
    
  10. In the main form, add a MenuItem object named New(MenuItem1), and another named Edit (MenuItem2). Add the following code for the New and EditClick events.

    ' Add new record.
    Private Sub MenuItem1_Click(ByVal sender As System.Object,_
      ByVal e As System.EventArgs) Handles MenuItem1.Click
        ProductsBindingSource.AllowNew = True
        ProductsBindingSource.AddNew()
    
        ' Pass the binding source to the form.
        Dim EditViewDialog As New EditView(ProductsBindingSource)
        If EditViewDialog.ShowDialog() <> DialogResult.OK Then
            ProductsBindingSource.CancelEdit()
        Else
            ProductsBindingSource.EndEdit()
            Me.ProductsTableAdapter.Update(Me.NorthwindDataSet)
        End If
    End Sub
    
    ' Edit record.
    Private Sub MenuItem2_Click(ByVal sender As System.Object,_
      ByVal e As System.EventArgs) Handles MenuItem2.Click
    
        ' Pass the binding source to the form.
        Dim EditViewDialog As New EditView(ProductsBindingSource)
        If EditViewDialog.ShowDialog() <> DialogResult.OK Then
            ProductsBindingSource.CancelEdit()
        Else
            ProductsBindingSource.EndEdit()
            Me.ProductsTableAdapter.Update(Me.NorthwindDataSet)
        End If
    End Sub
    
    // Add new record.
    private void menuItem1_Click(object sender, EventArgs e)
    {
        productsBindingSource.AllowNew = true;
        productsBindingSource.AddNew();
        EditView EditViewDialog = new EditView(productsBindingSource);
        if (EditViewDialog.ShowDialog() != DialogResult.OK)
        {
            productsBindingSource.CancelEdit();
        }
        else
        {
            ProductsBindingSource.EndEdit();
            this.productsTableAdapter.Update(this.northwindDataSet);
         }
    }
    // Edit record (Edit).
    private void menuItem2_Click(object sender, EventArgs e)
    {
        EditView EditViewDialog = new EditView(productsBindingSource);
        if (EditViewDialog.ShowDialog() != DialogResult.OK)
        {
            productsBindingSource.CancelEdit();
        }
        else
        {
            productsBindingSource.EndEdit();
            this.productsTableAdapter.Update(this.northwindDataSet);
        }
    }
    
  11. メイン フォームを KeyDownDataGrid コントロール、Pocket PC で、アクション キーが押されたときのイベントのコード追加します。 この操作は フォームの SummaryView のを表示します。

    ' Action button pressed.
    Private Sub DataGrid1_KeyDown(ByVal sender As System.Object, _
      ByVal e As System.Windows.Forms.KeyEventArgs) _
      Handles DataGrid1.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            Dim SummaryViewDialog As New SummaryView(ProductsBindingSource)
            SummaryViewDialog.ShowDialog()
        End If
    End Sub
    
    // Action button pressed.
    private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            SummaryView SummaryViewDialog = 
              new SummaryView(productsBindingSource);
            SummaryViewDialog.ShowDialog();
         }
    }
    

概要ビューを作成

  1. 次のコントロールを フォームの SummaryView に追加します。

    • Label 制御など、製品名見出しの"製品名:"です。

    • 製品名の値 Label コントロール。

    • Label のコントロールを [商品] テーブルの"生産中止"列の値が true ときにだけ表示される、"生産中止"の値。 このラベルの"DISCONTINUED"のタイトルします。赤いフォント。

  2. SummaryView フォーム データのバインドを設定するのにはコンストラクターに次のコードを追加します。 CurrentBindingSource フォームのコンストラクターに渡された BindingSource インスタンスに設定するをという名前のフォーム変数を宣言します。 DataRowView オブジェクトを true 生産中止] チェック ボックス列の場合、生産中止] チェック ボックスのラベルを表示するを決定します。

    'Dim CurrentBindingSource As BindingSource
    Public Sub New(ByVal bsource As BindingSource)
        CurrentBindingSource = bsource
        ' This call is required by the Windows Forms Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        ' Bind the label that shows the product name.
        ProductNameLabelVal.DataBindings.Add("Text", _
          CurrentBindingSource, "Product Name")
    
            ' Show the Discontinued label if
            ' that value is true in the database.
            Dim drView As DataRowView
            drView = CurrentBindingSource.Current
            If drView.Item("Discontinued") = True Then
                DiscontinuedLabel.Visible = True
            Else
                DiscontinuedLabel.Visible = False
            End If
    End Sub
    
    private BindingSource CurrentBindingSource;
    public SummaryView(BindingSource bsource)
    {
        CurrentBindingSource = bsource;
        InitializeComponent();
        // Bind the label that shows the product name.
        ProductNameLabelVal.DataBindings.Add("Text",
          CurrentBindingSource, "Product Name");
        // Show the Discontinued label if
        // that value is true in the database.
        DataRowView drView;
        drView = (DataRowView) CurrentBindingSource.Current;
        if (drView["Discontinued"] == true)
        {
            DiscontinuedLabel.Visible = true;
        }
        else
        {
            DiscontinuedLabel.Visible = false;
        }
    }
    

編集ビューを作成するには

  1. プロジェクトでの Microsoft.WindowsCE.Forms 名前空間に参照を追加します。

  2. InputPanel のツールボックスから フォームの EditView に、 コンポーネントをドラッグします。 テキスト ボックスにテキストを入力するユーザー、ソフト入力パネル (SIP) に提供するためにインスタンスを 1 つだけが必要です。

  3. フォームに次のコントロールを追加します。

    • Label に、製品名] テキスト ボックスのコントロール。

    • TextBox に、製品名] 列のコントロール。

    • "生産中止"列の CheckBox コントロール。 ThreeState プロパティを true に設定します。

  4. データ バインドを設定するには、フォームのコンストラクターに InitializeComponent 呼び出しの後に、次のコード追加します。 このコードは、新しいレコードを追加または既存のレコードを編集対応できます。 新しいレコードを追加するが場合、DataRowView オブジェクトを調べ販売を中止するかどうか列に Null 値が NullValue プロパティの Indeterminate 値のバインドの CheckState プロパティを設定します。

    Public Sub New(ByVal bsource As BindingSource)
        CurrentBindingSource = bsource
        InitializeComponent()
        ' Add the bindings.
        ProductNameTextBox.DataBindings.Add("Text",_
      CurrentBindingSource, "Product Name")
        Dim drView As DataRowView
        drView = CurrentBindingSource.Current
        If IsDBNull(drView("Discontinued")) Then
            DiscontinuedCheckBox.DataBindings.Add("CheckState",_
              CurrentBindingSource, "Discontinued", True,_
              DataSourceUpdateMode.OnValidation, _
              CheckState.Indeterminate)
        Else
            DiscontinuedCheckBox.DataBindings.Add("Checked",_
             CurrentBindingSource, "Discontinued")
        End If
    End Sub
    
    public EditView(BindingSource bsource)
    {
        CurrentBindingSource = bsource;
         InitializeComponent();
         CurrentBindingSource = bsource;
         InitializeComponent();
         //  Add the bindings.
         productNameTextBox.DataBindings.Add("Text",
           CurrentBindingSource, "Product Name");
         DataRowView drView;
         drView = (DataRowView) CurrentBindingSource.Current;
         if (drView("Discontinued") == null)
         {
             DiscontinuedCheckBox.DataBindings.Add("CheckState",
               CurrentBindingSource, "Discontinued", true,
               DataSourceUpdateMode.OnValidation,
               CheckState.Indeterminate);
         }
         else
         {
              DiscontinuedCheckBox.DataBindings.Add("Checked",
                CurrentBindingSource, "Discontinued");
         }
    }
    
  5. Add a MenuItem object titled Doneto update the database with the changes and return to the main form.

    ' Done
    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub
    // Done
    private void menuItem1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
    
  6. Add a MenuItem object titled Cancel,on the same level as Done, to discard the changes and return to the main form.

    ' Cancel
    Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
        Me.DialogResult = DialogResult.Cancel
        Me.Close()
    End Sub
    
    // Cancel
    private void menuItem1_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }
    

コードのコンパイル方法

この例では、次の名前空間への参照が必要です。

参照

処理手順

方法 : Smartphone 上で、データ グリッドを使用します。

概念

厳密に型指定された DataSet の生成 (ADO.NET)

その他の技術情報

データ アクセスおよび .NET Compact Framework における XML のサポート