DataGridTableStyle クラスとは? わかりやすく解説

DataGridTableStyle クラス

System.Windows.Forms.DataGrid コントロールによって実行時描画されるテーブル表します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Class DataGridTableStyle
    Inherits Component
    Implements IDataGridEditingService
Dim instance As DataGridTableStyle
public class DataGridTableStyle : Component,
 IDataGridEditingService
public ref class DataGridTableStyle : public
 Component, IDataGridEditingService
public class DataGridTableStyle extends Component
 implements IDataGridEditingService
public class DataGridTableStyle extends
 Component implements IDataGridEditingService
解説解説

System.Windows.Forms.DataGrid コントロールは、グリッド形式データ表示しますDataGridTableStyle は、描画されグリッドだけを表すクラスです。このグリッドは、グリッドデータ ソースとなる DataTable クラスとは区別する必要がありますDataGridTableStyle は、コントロール描画されたとおりに正確にグリッド表しますこのためDataGridTableStyle使用すると、各 DataTableグリッド外観制御できます特定の DataTableデータ表示するときに使用する DataGridTableStyle指定するには、MappingName を DataTable の TableName に設定します

TableStyles プロパティによって取得される GridTableStylesCollection には、System.Windows.Forms.DataGrid コントロール使用される DataGridTableStyle オブジェクトがすべて格納されます。このコレクションには、必要な数の DataGridTableStyle オブジェクト格納できますが、各オブジェクトMappingName一意である必要があります。これにより、同じデータに対してユーザー設定に応じて実行時異なDataGridTableStyle適用できます上記の処理を実現するには、次の操作実行します

  1. GridTableStylesCollectionDataGridTableStyle オブジェクト格納しますDataGridTableStyleGridTableStylesCollection 内に存在し、その MappingName プロパティ値が DataTable オブジェクトTableName プロパティ等し場合DataTable はこの DataGridTableStyle使用して表示されます。一致する MappingName 値を持つ DataGridTableStyle存在しない場合DataTableデータ グリッド テーブル既定スタイル使用して表示されます。

  2. 別のグリッド スタイル必要な場合は、Item プロパティ使用して適切な DataGridTableStyle選択し (TableNameItem プロパティに渡す)、返されオブジェクトMappingName新しい値に設定します

  3. Item プロパティ使用して必要な DataGridTableStyle選択し、その MappingNameDataTableTableName設定します

注意に関するメモ注意

必ず DataGridColumnStyle オブジェクト作成して GridColumnStylesCollection に追加してから、DataGridTableStyle オブジェクトGridTableStylesCollection追加します有効な MappingName 値を持つ空の DataGridTableStyleコレクション追加すると、自動的に DataGridColumnStyle オブジェクト生成されます。そのため、MappingName 値が重複する新しDataGridColumnStyle オブジェクトGridColumnStylesCollection追加しようとすると、例外スローさます。

現在表示されている DataGridTableStyle確認するには、System.Windows.Forms.DataGrid の DataSource プロパティと DataMember プロパティ使用して、CurrencyManager を返しますデータ ソースに ITypedList インターフェイス実装されている場合は、GetListName メソッド使用すると、現在のテーブルMappingName返すことができます上記の例を次の C# コード示します

 private void PrintCurrentListName(DataGrid myDataGrid){
  CurrencyManager myCM = (CurrencyManager)
  BindingContext[myDataGrid.DataSource, myDataGrid.DataMember];
  IList myList = myCM.List;
  ITypedList thisList = (ITypedList) myList;
  Console.WriteLine(thisList.GetListName(null));
 }

DataSet 内に DataRelation オブジェクトによって関連付けられている複数DataTable オブジェクト格納されていて、現在表示されている DataTable が子テーブルである場合DataMember は、TableName.RelationName という形式 (最も簡単な場合) の文字列返しますDataTable階層構造内のさらに下位位置する場合、この文字列は、親テーブルの名前に、そのテーブルレベル到達するために必要な RelationName 値が続く形で表されます。たとえば、階層的なリレーションシップ持ち、上から順に RegionsCustomers、および Orders という名前が付いている 3 つの DataTable オブジェクトと、RegionsToCustomers および CustomersToOrders という名前の 2 つDataRelation オブジェクトがある場合DataMember プロパティは、"Regions.RegionsToCustomers.CustomersToOrders" を返します。ただし、この場合MappingName は "Orders" になります

DataGridTableStyle オブジェクトコレクションは、System.Windows.Forms.DataGridTableStyles プロパティによって返されます。

DataGridTableStyle表示されると、その DataGridTableStyle設定値によって System.Windows.Forms.DataGrid コントロール設定値オーバーライドされます特定の DataGridTableStyle プロパティに値が設定されていない場合は、代わりに System.Windows.Forms.DataGrid コントロールの値が使用されます。System.Windows.Forms.DataGrid コントロールプロパティオーバーライドするために設定できる DataGridColumnStyle プロパティ次に示します

  • AllowSorting

  • AlternatingBackColor

  • BackColor

  • ColumnHeadersVisible

  • ForeColor

  • GridLineColor

  • GridLineStyle

  • HeaderBackColor

  • HeaderFont

  • HeaderForeColor

  • LinkColor

  • PreferredColumnWidth

  • PreferredRowHeight

  • ReadOnly

  • RowHeadersVisible

  • RowHeaderWidth

  • SelectionBackColor

  • SelectionForeColor

DataGridオブジェクト厳密に指定され配列バインドするには、オブジェクト型パブリック プロパティ含まれている必要があります配列表示する DataGridTableStyle作成するには、DataGridTableStyle.MappingName プロパティtypename設定します。なお、この typename実際オブジェクト型名に置き換えてくださいまた、MappingName プロパティでは大文字と小文字区別されることに注意してください。型の名前は正確に指定する必要があります例については、MappingName プロパティトピック参照してください

また、DataGrid を ArrayList にバインドできますArrayList特長は、複数の型のオブジェクト格納できることです。ただし、DataGridリスト内のすべての項目の型が 1 番目の項目の型と同じ場合限りリストバインドできます。つまり、すべてのオブジェクトの型が同じであるか、リスト内の最初の項目と同じクラスからすべてのクラス継承している必要があります。たとえば、リスト内の最初の項目が Control場合2 番目の項目は (Control から継承した) TextBoxできます逆に、1 番目の項目が TextBox場合2 番目のオブジェクトControl になることはできません。さらに、ArrayListバインドされている場合には項目が含まれている必要があり、DataGridTableStyle 内のオブジェクトパブリック プロパティ含まれている必要があります。空の ArrayList場合、空のグリッドとなりますArrayListバインディングするときは、DataGridTableStyleMappingName を "ArrayList" (型の名前) に設定します

使用例使用例

2 つDataGridTableStyle インスタンス作成し、各オブジェクトMappingNameDataSet 内の DataTableTableName設定するコード例次に示します。さらに、この例では DataGridColumnStyle オブジェクトを各 DataGridTableStyle オブジェクトGridColumnStylesCollection追加してます。実行例については、System.Windows.Forms.DataGridトピック参照してください

Private Sub AddCustomDataTableStyle()
    ' Create a new DataGridTableStyle and set
    ' its MappingName to the TableName of a DataTable. 
    Dim ts1 As New DataGridTableStyle()
    ts1.MappingName = "Customers"
    
    ' Add a GridColumnStyle and set its MappingName
    ' to the name of a DataColumn in the DataTable.
    ' Set the HeaderText and Width properties. 
    
    Dim boolCol As New DataGridBoolColumn()
    boolCol.MappingName = "Current"
    boolCol.HeaderText = "IsCurrent Customer"
    boolCol.Width = 150
    ts1.GridColumnStyles.Add(boolCol)
    
    ' Add a second column style.
    Dim TextCol As New DataGridTextBoxColumn()
    TextCol.MappingName = "custName"
    TextCol.HeaderText = "Customer Name"
    TextCol.Width = 250
    ts1.GridColumnStyles.Add(TextCol)

    ' Create the second table style with columns.
    Dim ts2 As New DataGridTableStyle()
    ts2.MappingName = "Orders"

    ' Change the colors.
    ts2.ForeColor = Color.Yellow
    ts2.AlternatingBackColor = Color.Blue
    ts2.BackColor = Color.Blue
    
    ' Create new DataGridColumnStyle objects.
    Dim cOrderDate As New
 DataGridTextBoxColumn()
    cOrderDate.MappingName = "OrderDate"
    cOrderDate.HeaderText = "Order Date"
    cOrderDate.Width = 100
    ts2.GridColumnStyles.Add(cOrderDate)
    
    Dim pcol As PropertyDescriptorCollection
 = Me.BindingContext(myDataSet, "Customers.custToOrders").GetItemProperties()
    
    Dim csOrderAmount As New
 DataGridTextBoxColumn(pcol("OrderAmount"), "c",
 True)
    csOrderAmount.MappingName = "OrderAmount"
    csOrderAmount.HeaderText = "Total"
    csOrderAmount.Width = 100
    ts2.GridColumnStyles.Add(csOrderAmount)

    ' Add the DataGridTableStyle objects to the collection.
    myDataGrid.TableStyles.Add(ts1)
    myDataGrid.TableStyles.Add(ts2)
End Sub 'AddCustomDataTableStyle
private void AddCustomDataTableStyle()
   {
      /* Create a new DataGridTableStyle and set
      its MappingName to the TableName of a DataTable. */
      DataGridTableStyle ts1 = new DataGridTableStyle();
      ts1.MappingName = "Customers";

      /* Add a GridColumnStyle and set its MappingName 
      to the name of a DataColumn in the DataTable. 
      Set the HeaderText and Width properties. */
      
      DataGridColumnStyle boolCol = new DataGridBoolColumn();
      boolCol.MappingName = "Current";
      boolCol.HeaderText = "IsCurrent Customer";
      boolCol.Width = 150;
      ts1.GridColumnStyles.Add(boolCol);
      
      // Add a second column style.
      DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
      TextCol.MappingName = "custName";
      TextCol.HeaderText = "Customer Name";
      TextCol.Width = 250;
      ts1.GridColumnStyles.Add(TextCol);


      // Create the second table style with columns.
      DataGridTableStyle ts2 = new DataGridTableStyle();
      ts2.MappingName = "Orders";
      // Change the colors.
      ts2.ForeColor = Color.Yellow;
      ts2.AlternatingBackColor = Color.Blue;
      ts2.BackColor = Color.Blue;
      
      // Create new DataGridColumnStyle objects.
      DataGridColumnStyle cOrderDate = 
      new DataGridTextBoxColumn();
      cOrderDate.MappingName = "OrderDate";
      cOrderDate.HeaderText = "Order Date";
      cOrderDate.Width = 100;
      ts2.GridColumnStyles.Add(cOrderDate);

      PropertyDescriptorCollection pcol = this.BindingContext
      [myDataSet, "Customers.custToOrders"].GetItemProperties();
      
      DataGridColumnStyle csOrderAmount = 
      new DataGridTextBoxColumn(pcol["OrderAmount"],
 "c", true);
      csOrderAmount.MappingName = "OrderAmount";
      csOrderAmount.HeaderText = "Total";
      csOrderAmount.Width = 100;
      ts2.GridColumnStyles.Add(csOrderAmount);

      // Add the DataGridTableStyle objects to the collection.
      myDataGrid.TableStyles.Add(ts1);
      myDataGrid.TableStyles.Add(ts2);
   }

void AddCustomDataTableStyle()
{
   
   /* Create a new DataGridTableStyle and set
         its MappingName to the TableName of a DataTable. */
   DataGridTableStyle^ ts1 = gcnew DataGridTableStyle;
   ts1->MappingName = "Customers";
   
   /* Add a GridColumnStyle and set its MappingName 
         to the name of a DataColumn in the DataTable. 
         Set the HeaderText and Width properties. */
   DataGridColumnStyle^ boolCol = gcnew DataGridBoolColumn;
   boolCol->MappingName = "Current";
   boolCol->HeaderText = "IsCurrent Customer";
   boolCol->Width = 150;
   ts1->GridColumnStyles->Add( boolCol );
   
   // Add a second column style.
   DataGridColumnStyle^ TextCol = gcnew DataGridTextBoxColumn;
   TextCol->MappingName = "custName";
   TextCol->HeaderText = "Customer Name";
   TextCol->Width = 250;
   ts1->GridColumnStyles->Add( TextCol );
   
   // Create the second table style with columns.
   DataGridTableStyle^ ts2 = gcnew DataGridTableStyle;
   ts2->MappingName = "Orders";
   
   // Change the colors.
   ts2->ForeColor = Color::Yellow;
   ts2->AlternatingBackColor = Color::Blue;
   ts2->BackColor = Color::Blue;
   
   // Create new DataGridColumnStyle objects.
   DataGridColumnStyle^ cOrderDate = gcnew DataGridTextBoxColumn;
   cOrderDate->MappingName = "OrderDate";
   cOrderDate->HeaderText = "Order Date";
   cOrderDate->Width = 100;
   ts2->GridColumnStyles->Add( cOrderDate );
   PropertyDescriptorCollection^ pcol = this->BindingContext[
 myDataSet,"Customers.custToOrders" ]->GetItemProperties();
   DataGridColumnStyle^ csOrderAmount = gcnew DataGridTextBoxColumn( pcol[ "OrderAmount"
 ],"c",true );
   csOrderAmount->MappingName = "OrderAmount";
   csOrderAmount->HeaderText = "Total";
   csOrderAmount->Width = 100;
   ts2->GridColumnStyles->Add( csOrderAmount );
   
   // Add the DataGridTableStyle objects to the collection.
   myDataGrid->TableStyles->Add( ts1 );
   myDataGrid->TableStyles->Add( ts2 );
}

private void AddCustomDataTableStyle()
{
    /*  Create a new DataGridTableStyle and set
        its MappingName to the TableName of a DataTable. 
     */
    DataGridTableStyle ts1 = new DataGridTableStyle();
    ts1.set_MappingName("Customers");
    /*  Add a GridColumnStyle and set its MappingName 
        to the name of a DataColumn in the DataTable. 
        Set the HeaderText and Width properties. 
     */
    
    DataGridColumnStyle boolCol = new DataGridBoolColumn();
    boolCol.set_MappingName("Current");
    boolCol.set_HeaderText("IsCurrent Customer");
    boolCol.set_Width(150);
    ts1.get_GridColumnStyles().Add(boolCol);

    // Add a second column style.
    DataGridColumnStyle textCol = new DataGridTextBoxColumn();
    textCol.set_MappingName("custName");
    textCol.set_HeaderText("Customer Name");
    textCol.set_Width(250);
    ts1.get_GridColumnStyles().Add(textCol);

    // Create the second table style with columns.
    DataGridTableStyle ts2 = new DataGridTableStyle();
    ts2.set_MappingName("Orders");
    // Change the colors.
    ts2.set_ForeColor(Color.get_Yellow());
    ts2.set_AlternatingBackColor(Color.get_Blue());
    ts2.set_BackColor(Color.get_Blue());

    // Create new DataGridColumnStyle objects.
    DataGridColumnStyle cOrderDate = new DataGridTextBoxColumn();
    cOrderDate.set_MappingName("OrderDate");
    cOrderDate.set_HeaderText("Order Date");
    cOrderDate.set_Width(100);
    ts2.get_GridColumnStyles().Add(cOrderDate);

    PropertyDescriptorCollection pcol = 
        this.get_BindingContext().
        get_Item(myDataSet, "Customers.custToOrders").
        GetItemProperties();
    DataGridColumnStyle csOrderAmount = 
        new DataGridTextBoxColumn(pcol.get_Item("OrderAmount"),
 "c", true);
    csOrderAmount.set_MappingName("OrderAmount");
    csOrderAmount.set_HeaderText("Total");
    csOrderAmount.set_Width(100);
    ts2.get_GridColumnStyles().Add(csOrderAmount);

    // Add the DataGridTableStyle objects to the collection.
    myDataGrid.get_TableStyles().Add(ts1);
    myDataGrid.get_TableStyles().Add(ts2);
} //AddCustomDataTableStyle
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
      System.Windows.Forms.DataGridTableStyle
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataGridTableStyle メンバ
System.Windows.Forms 名前空間
DataGrid クラス
DataGridColumnStyle クラス
GridColumnStylesCollection
GridTableStylesCollection



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DataGridTableStyle クラス」の関連用語

DataGridTableStyle クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataGridTableStyle クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS