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

注意

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

GridColumnStylesCollection オブジェクトと DataGridColumnStyle オブジェクト (DataGridTableStyle クラスのメンバー) のプロパティとメソッドを使用して、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;  
    }  
    

関連項目