DataGridTableStyle クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


System.Windows.Forms.DataGrid コントロールは、グリッド形式でデータを表示します。DataGridTableStyle は、描画されたグリッドだけを表すクラスです。このグリッドは、グリッドのデータ ソースとなる DataTable クラスとは区別する必要があります。DataGridTableStyle は、コントロールで描画されたとおりに正確にグリッドを表します。このため、DataGridTableStyle を使用すると、各 DataTable のグリッドの外観を制御できます。特定の DataTable のデータを表示するときに使用する DataGridTableStyle を指定するには、MappingName を DataTable の TableName に設定します。
TableStyles プロパティによって取得される GridTableStylesCollection には、System.Windows.Forms.DataGrid コントロールで使用される DataGridTableStyle オブジェクトがすべて格納されます。このコレクションには、必要な数の DataGridTableStyle オブジェクトを格納できますが、各オブジェクトの MappingName は一意である必要があります。これにより、同じデータに対して、ユーザー設定に応じて実行時に異なる DataGridTableStyle を適用できます。上記の処理を実現するには、次の操作を実行します。
-
GridTableStylesCollection に DataGridTableStyle オブジェクトを格納します。DataGridTableStyle が GridTableStylesCollection 内に存在し、その MappingName プロパティ値が DataTable オブジェクトの TableName プロパティと等しい場合、DataTable はこの DataGridTableStyle を使用して表示されます。一致する MappingName 値を持つ DataGridTableStyle が存在しない場合、DataTable はデータ グリッド テーブルの既定のスタイルを使用して表示されます。
-
別のグリッド スタイルが必要な場合は、Item プロパティを使用して、適切な DataGridTableStyle を選択し (TableName を Item プロパティに渡す)、返されたオブジェクトの MappingName を新しい値に設定します。
-
Item プロパティを使用して必要な DataGridTableStyle を選択し、その MappingName を DataTable の TableName に設定します。
![]() |
---|
必ず 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 値が続く形で表されます。たとえば、階層的なリレーションシップを持ち、上から順に Regions、Customers、および Orders という名前が付いている 3 つの DataTable オブジェクトと、RegionsToCustomers および CustomersToOrders という名前の 2 つの DataRelation オブジェクトがある場合、DataMember プロパティは、"Regions.RegionsToCustomers.CustomersToOrders" を返します。ただし、この場合の MappingName は "Orders" になります。
DataGridTableStyle オブジェクトのコレクションは、System.Windows.Forms.DataGrid の TableStyles プロパティによって返されます。
DataGridTableStyle が表示されると、その DataGridTableStyle の設定値によって System.Windows.Forms.DataGrid コントロールの設定値がオーバーライドされます。特定の DataGridTableStyle プロパティに値が設定されていない場合は、代わりに System.Windows.Forms.DataGrid コントロールの値が使用されます。System.Windows.Forms.DataGrid コントロールのプロパティをオーバーライドするために設定できる DataGridColumnStyle プロパティを次に示します。
-
AllowSorting
-
AlternatingBackColor
-
ColumnHeadersVisible
-
ForeColor
-
GridLineColor
-
GridLineStyle
-
HeaderBackColor
-
HeaderFont
-
HeaderForeColor
-
LinkColor
-
PreferredColumnWidth
-
PreferredRowHeight
-
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 にバインディングするときは、DataGridTableStyle の MappingName を "ArrayList" (型の名前) に設定します。

2 つの DataGridTableStyle インスタンスを作成し、各オブジェクトの MappingName を DataSet 内の DataTable の TableName に設定するコード例を次に示します。さらに、この例では 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.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.DataGridTableStyle


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DataGridTableStyle クラスのページへのリンク