次の方法で共有


方法 : Windows フォーム DataGrid コントロールの列を削除するまたは非表示にする

注意

DataGridView コントロールは、DataGrid コントロールに代わると共に追加の機能を提供します。ただし、DataGrid コントロールは、下位互換性を保つ目的および将来使用する目的で保持されます。 詳細については、「Windows フォームの DataGridView コントロールと DataGrid コントロールの違いについて」を参照してください。

DataGridTableStyle クラスのメンバーである GridColumnStylesCollection オブジェクトと DataGridColumnStyle オブジェクトのプロパティおよびメソッドを使用して、Windows フォーム DataGrid コントロールの列をプログラムで削除したり非表示にしたりできます。

削除した列や非表示にした列は、グリッドが連結されているデータ ソースに引き続き含まれていて、プログラムでアクセスできます。 単にデータ グリッドに表示されないだけです。

注意

アプリケーションがデータの特定の列にアクセスせず、それらの列をデータ グリッドに表示する必要がないのであれば、通常はそれらの列をデータ ソースに含める必要性はありません。

プログラムで DataGrid から列を削除するには

  1. フォームの宣言領域で、DataGridTableStyle クラスの新しいインスタンスを宣言します。

  2. DataGridTableStyle.MappingName プロパティに、スタイルを適用するデータ ソース内のテーブルを設定します。 次の例では、DataGrid.DataMember プロパティを使用しています。このプロパティは既に設定されていると仮定しています。

  3. データ グリッドのテーブル スタイル コレクションに新しい DataGridTableStyle オブジェクトを追加します。

  4. 削除する列の列インデックスを指定して、DataGridGridColumnStyles コレクションの RemoveAt メソッドを呼び出します。

    ' Declare a new DataGridTableStyle in the
    ' declarations area of your form.
    Dim ts As DataGridTableStyle = New DataGridTableStyle()
    
    Sub DeleteColumn()
       ' Set the DataGridTableStyle.MappingName property
       ' to the table in the data source to map to.
       ts.MappingName = DataGrid1.DataMember
    
       ' Add it to the datagrid's TableStyles collection
       DataGrid1.TableStyles.Add(ts)
    
       ' Delete the first column (index 0)
       DataGrid1.TableStyles(0).GridColumnStyles.RemoveAt(0)
    End Sub
    
    // Declare a new DataGridTableStyle in the
    // declarations area of your form.
    DataGridTableStyle ts = new DataGridTableStyle();
    
    private void deleteColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;
    
       // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);
    
       // Delete the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles.RemoveAt(0);
    }
    

プログラムで DataGrid の列を非表示にするには

  1. フォームの宣言領域で、DataGridTableStyle クラスの新しいインスタンスを宣言します。

  2. DataGridTableStyleMappingName プロパティに、スタイルを適用するデータ ソース内のテーブルを設定します。 次のコード例では、DataGrid.DataMember プロパティを使用しています。このプロパティは既に設定されていると仮定しています。

  3. データ グリッドのテーブル スタイル コレクションに新しい DataGridTableStyle オブジェクトを追加します。

  4. 非表示にする列の列インデックスを指定して Width プロパティを 0 に設定すると、列が非表示になります。

    ' Declare a new DataGridTableStyle in the
    ' declarations area of your form.
    Dim ts As DataGridTableStyle = New DataGridTableStyle()
    
    Sub HideColumn()
       ' Set the DataGridTableStyle.MappingName property
       ' to the table in the data source to map to.
       ts.MappingName = DataGrid1.DataMember
    
       ' Add it to the datagrid's TableStyles collection
       DataGrid1.TableStyles.Add(ts)
    
       ' Hide the first column (index 0)
       DataGrid1.TableStyles(0).GridColumnStyles(0).Width = 0
    End Sub
    
    // Declare a new DataGridTableStyle in the
    // declarations area of your form.
    DataGridTableStyle ts = new DataGridTableStyle();
    
    private void hideColumn()
    {
       // Set the DataGridTableStyle.MappingName property
       // to the table in the data source to map to.
       ts.MappingName = dataGrid1.DataMember;
    
       // Add it to the datagrid's TableStyles collection
       dataGrid1.TableStyles.Add(ts);
    
       // Hide the first column (index 0)
       dataGrid1.TableStyles[0].GridColumnStyles[0].Width = 0;
    }
    

参照

処理手順

方法 : Windows フォーム DataGrid コントロールに表示されるデータを実行時に変更する

方法 : Windows フォーム DataGrid コントロールにテーブルと列を追加する