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

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

Smartphone アプリケーションと同様、Smartphone 連絡先 プログラムに作成できます。

注意

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

DataGrid Visual Studio と一緒にインストールされる、Northwind データベースからコントロール内の商品名の一覧がメイン フォーム例です。 また、現在のレコードを表示する概要ビュー フォームと、エディット ビューのフォーム データを編集して新しいレコードを追加するも含まれています。 BindingSource オブジェクトをデータベース内で現在選択されているレコードにアクセスできます。 データ連結コントロール、に加えて BindingSource オブジェクトを現在の行の DataRowView オブジェクトを返すことができます。 現在の列の値を決定するなど、目的のさまざまなデータにアクセスする DataRowView を使用できます。

また、Visual Studio サマリーを生成してデータ フォームを生成 を DataGrid コントロールにスマート タグのショートカット メニューから選択するフォームを自動的に編集ができます。 注意デモのために、2 つの列は、集計用の次の使用例で使用してくださいフォームを編集します。

このアプリケーションが、フォームの次の表にしています。 その] メニューの [オプション、Smartphone 左と右ソフト キーはも一覧に表示です。

Form

機能

左側のソフト キー

右ソフト キー

メイン フォーム

(Form1)

DataGrid コントロール Smartphone の連絡先リストのスタイルのテーブルの列に 1 つ表示します。

の操作のキーを押してキー、またはエミュレーター、キーボードの Enter の概要表示フォームを表示します。

新規

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

編集

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

SummaryView

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

チェック

メイン フォームに戻ります。

(なし)

EditView

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

チェック

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

[キャンセル]

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

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

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

  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 its columns.

  5. のツールボックス、 DataGrid コントロールをフォームに追加します。

  6. Smartphone の DataGrid 連絡先 一覧のように表示される、 コントロールに、次の表に従ってプロパティを設定します。

    データ グリッドのプロパティ

    設定するには

    ColumnHeadersVisible

    False

    RowHeadersVisible

    False

    GridLineColor

    Window

    Location

    Point 構造体の x、-2 と y の -2 です。

    Size

    183 の幅と高さの 190 Size 構造体です。

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

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

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

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

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

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

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

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

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

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

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

  10. 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();
    }
    
  11. In the main form, add a MenuItem object named New(MenuItem1), and another named Edit (MenuItem2). これらのメニューは、Smartphone 左と右ソフト キーに対応します。 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.AddNew()
        Dim EditViewDialog As New EditView(ProductsBindingSource)
        If EditViewDialog.ShowDialog() <> DialogResult.OK Then
            ProductsBindingSource.CancelEdit()
        Else
            ProductsBindingSource.EndEdit()
            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 MenuItem1.Click
        Dim EditViewDialog As New EditView(ProductsBindingSource)
        If EditViewDialog.ShowDialog() <> DialogResult.OK Then
            ProductsBindingSource.CancelEdit()
        Else
            ProductsBindingSource.EndEdit()
            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);
        }
    }
    
  12. メイン フォーム内でコード、Smartphone のアクション キーが押されたときに発生する KeyDown イベントを追加します。 この操作は フォームの SummaryView のを表示します。

    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 SummaryView = New SummaryView(ProductsBindingSource)
            Cursor.Current = Cursors.Default
            SummaryView.ShowDialog()
        End If
    End Sub
    
    private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            SummaryView SummaryViewDialog = 
              new SummaryView(productsBindingSource);
            SummaryViewDialog.ShowDialog();
         }
    }
    

概要ビューを作成

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

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

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

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

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

    Public Sub New(ByVal bsource As BindingSource)
        CurrentBindingSource = bsource
    
        ' This call is required by the Windows Forms Designer.
        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.
            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;
        }
    }
    
  3. MenuItem 左ソフト キーのフォームを閉じ、メイン フォームに戻って完了という オブジェクトを追加します。

    Private Sub MenuItem1_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles MenuItem1.Click
        Me.Close
    End Sub
    
    private void MenuItem1_Click(object sender, System.EventArgs e)
    {
        this.Close();
    }
    

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

  1. プロジェクトでの Microsoft.WindowsCE.Forms 名前空間に参照を追加します。 テキスト ボックス コントロールで、Smartphone InputMode 設定を設定するこれを必要があります。

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

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

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

    • "生産中止"の値 CheckBox コントロール。

  3. データ バインドを設定するには、フォームのコンストラクターに InitializeComponent 呼び出しの後に、次のコード追加します。 このコードは、新しいレコードを追加または既存のレコードを編集対応できます。 新しいレコードを追加されている場合、DataRowView オブジェクトは"生産中止"の列が NULL 値をかどうかを調べます。 値が null の場合場合は、チェック ボックスは false に設定されます。

    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.
    
        ' Set the Smartphone input mode.
        InputModeEditor.SetInputMode(ProductNameTextBox,_
          InputMode.AlphaT9)
        ProductNameTextBox.DataBindings.Add("Text",_
          CurrentBindingSource, "Product Name")
    
        ' Determine the Discontinued value.
        ' If null, change to False.
        Dim drView As DataRowView
        drView = CurrentBindingSource.Current
        ' Set the bindings.
        If IsDBNull(drView("Discontinued")) Then
            DiscontinuedCheckBox.DataBindings.Add("CheckState",_
              CurrentBindingSource, "Discontinued", True,_
              DataSourceUpdateMode.OnValidation, False, "")
        Else
            DiscontinuedCheckBox.DataBindings.Add("Checked",_
              CurrentBindingSource, "Discontinued")
         End If
    End Sub
    
    public EditView(BindingSource bsource)
    {
        CurrentBindingSource = bsource;
        InitializeComponent();
        // Set the Smartphone input mode.
        InputModeEditor.SetInputMode(ProductNameTextBox,
          InputMode.AlphaT9);
        // Set the bindings.
        ProductNameTextBox.DataBindings.Add("Text",
          CurrentBindingSource,"Product Name");
        // Determine the Discontinued value.
        // If null, change to False.
        DataRowView drView;
        drView = (DataRowView) CurrentBindingSource.Current;
        if(drView("Discontinued")== null)
        {
            DiscontinuedCheckBox.DataBindings.Add("CheckState",
              CurrentBindingSource, "Discontinued",
              true,DataSourceUpdateMode.OnValidation,false,"");
        }
        else
        {
            DiscontinuedCheckBox.DataBindings.Add("Checked",
              CurrentBindingSource, "Discontinued");
        }
    }
    
  1. Add a MenuItem object titled Donefor the left soft key to update the database with the changes and return to the Main Form.

    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
    
    Private void MenuItem1_Click(object sender, System.EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
    
  1. Add a MenuItem object titled Cancelfor the right soft key to discard the changes and return to the main form.

    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
    
    Private void MenuItem2_Click(object sender, System.EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }
    

コードのコンパイル方法

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

参照

処理手順

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

概念

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

その他の技術情報

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