GridTableStylesCollection 類別

定義

代表 DataGridTableStyle 控制項中 DataGrid 物件的集合。

public ref class GridTableStylesCollection : System::Windows::Forms::BaseCollection, System::Collections::IList
[System.ComponentModel.ListBindable(false)]
public class GridTableStylesCollection : System.Windows.Forms.BaseCollection, System.Collections.IList
[<System.ComponentModel.ListBindable(false)>]
type GridTableStylesCollection = class
    inherit BaseCollection
    interface IList
    interface ICollection
    interface IEnumerable
Public Class GridTableStylesCollection
Inherits BaseCollection
Implements IList
繼承
GridTableStylesCollection
屬性
實作

範例

下列程式碼範例會建立兩 DataGridTableStyleDataGrid 物件,並將每個 物件新增至 GridTableStylesCollection 控制項的 屬性所傳 TableStyles 回的 。

void AddCustomDataTableStyle()
{
   DataGridTableStyle^ ts1 = gcnew DataGridTableStyle;
   ts1->MappingName = "Customers";
   
   // Set other properties.
   ts1->AlternatingBackColor = Color::LightGray;
   
   /* 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";
   
   // Set other properties.
   ts2->AlternatingBackColor = Color::LightBlue;
   
   // Create new ColumnStyle objects.
   DataGridColumnStyle^ cOrderDate = gcnew DataGridTextBoxColumn;
   cOrderDate->MappingName = "OrderDate";
   cOrderDate->HeaderText = "Order Date";
   cOrderDate->Width = 100;
   ts2->GridColumnStyles->Add( cOrderDate );
   
   /*Use a PropertyDescriptor to create a formatted
     column. First get the PropertyDescriptorCollection
     for the data source and data member. */
   System::ComponentModel::PropertyDescriptorCollection^ pcol = this->
       BindingContext[myDataSet, "Customers::custToOrders"]->
       GetItemProperties();
   
   /* Create a formatted column using a PropertyDescriptor.
     The formatting character S"c" specifies a currency format. */
   DataGridColumnStyle^ csOrderAmount =
      gcnew DataGridTextBoxColumn( pcol[ "OrderAmount" ],"c",true );
   csOrderAmount->MappingName = "OrderAmount";
   csOrderAmount->HeaderText = "Total";
   csOrderAmount->Width = 100;
   ts2->GridColumnStyles->Add( csOrderAmount );
   
   /* Add the DataGridTableStyle instances to
     the GridTableStylesCollection. */
   myDataGrid->TableStyles->Add( ts1 );
   myDataGrid->TableStyles->Add( ts2 );
}
private void AddCustomDataTableStyle(){
   DataGridTableStyle ts1 = new DataGridTableStyle();
   ts1.MappingName = "Customers";
   // Set other properties.
   ts1.AlternatingBackColor = Color.LightGray;

   /* 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";

   // Set other properties.
   ts2.AlternatingBackColor = Color.LightBlue;
   
   // Create new ColumnStyle objects.
   DataGridColumnStyle cOrderDate = 
   new DataGridTextBoxColumn();
   cOrderDate.MappingName = "OrderDate";
   cOrderDate.HeaderText = "Order Date";
   cOrderDate.Width = 100;
   ts2.GridColumnStyles.Add(cOrderDate);

   /*Use a PropertyDescriptor to create a formatted
   column. First get the PropertyDescriptorCollection
   for the data source and data member. */
   System.ComponentModel.PropertyDescriptorCollection pcol = 
      this.BindingContext[myDataSet, "Customers.custToOrders"]
      .GetItemProperties();
 
   /* Create a formatted column using a PropertyDescriptor.
   The formatting character "c" specifies a currency format. */     
   DataGridColumnStyle csOrderAmount = 
   new DataGridTextBoxColumn(pcol["OrderAmount"], "c", true);
   csOrderAmount.MappingName = "OrderAmount";
   csOrderAmount.HeaderText = "Total";
   csOrderAmount.Width = 100;
   ts2.GridColumnStyles.Add(csOrderAmount);

   /* Add the DataGridTableStyle instances to 
   the GridTableStylesCollection. */
   myDataGrid.TableStyles.Add(ts1);
   myDataGrid.TableStyles.Add(ts2);
}
Private Sub AddCustomDataTableStyle()
   Dim ts1 As New DataGridTableStyle()
   ts1.MappingName = "Customers"
   ' Set other properties.
   ts1.AlternatingBackColor = Color.LightGray
   ' 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"
     
   ' Set other properties.
   ts2.AlternatingBackColor = Color.LightBlue
     
   ' Create new ColumnStyle objects.
   Dim cOrderDate As New DataGridTextBoxColumn()
   cOrderDate.MappingName = "OrderDate"
   cOrderDate.HeaderText = "Order Date"
   cOrderDate.Width = 100
   ts2.GridColumnStyles.Add(cOrderDate)

   ' Use a PropertyDescriptor to create a formatted
   ' column. First get the PropertyDescriptorCollection
   ' for the data source and data member. 
   Dim pcol As System.ComponentModel.PropertyDescriptorCollection = _
   Me.BindingContext(myDataSet, "Customers.custToOrders"). _
   GetItemProperties()

   ' Create a formatted column using a PropertyDescriptor.
   ' The formatting character "c" specifies a currency format. */     
     
   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 instances to 
   ' the GridTableStylesCollection. 
   myDataGrid.TableStyles.Add(ts1)
   myDataGrid.TableStyles.Add(ts2)
End Sub

備註

GridTableStylesCollection包含 DataGridTableStyle 物件,可讓 DataGrid 控制項顯示 中 DataSet 每個 DataTable 的自訂格線樣式。

在 控制項上 DataGridTableStyles 屬性會傳 GridTableStylesCollection 回 。

根據預設, GridTableStylesCollection 不包含任何 DataGridTableStyle 物件。 相反地,會 DataGrid 使用色彩、寬度和格式的預設設定來顯示每個表格。 每個資料表的所有資料行都會顯示。 DataGridTableStyle將 加入至集合時,會 DataGrid 使用 MappingName 來判斷哪一個物件提供方格的資料。 例如,如果資料來源是包含三 DataTableDataSet 物件的 ,則必須 MappingName 符合 TableName 其中一個 物件的 。 MappingName如果 不符合任何 TableName 值,則會使用預設設定來顯示每個資料表的資料,而且 DataGridTableStyle 將會忽略設定。

警告

請一律建立 DataGridColumnStyle 物件,並在將 物件新增至 GridColumnStylesCollection 之前將其新增 DataGridTableStyleGridTableStylesCollection 。 當您將具有有效 MappingName 值的空白 DataGridTableStyle 新增至集合時, DataGridColumnStyle 會自動為您產生 物件。 因此,如果您嘗試將具有重複 MappingName 值的新 DataGridColumnStyle 物件加入至 GridColumnStylesCollection ,將會擲回例外狀況。 或者,使用 Clear 方法清除 GridColumnStylesCollection

屬性

Count

取得集合中的元素總數。

(繼承來源 BaseCollection)
IsReadOnly

取得值,表示集合是否為唯讀。

(繼承來源 BaseCollection)
IsSynchronized

取得值,表示對 ICollection 的存取是否同步化。

(繼承來源 BaseCollection)
Item[Int32]

取得索引所指定的 DataGridTableStyle

Item[String]

取得具有指定名稱的 DataGridTableStyle

List

取得基礎清單。

SyncRoot

取得可用以同步存取 BaseCollection 的物件。

(繼承來源 BaseCollection)

方法

Add(DataGridTableStyle)

DataGridTableStyle 新增至這個集合中。

AddRange(DataGridTableStyle[])

將資料表樣式陣列加入集合中。

Clear()

清除集合。

Contains(DataGridTableStyle)

取得值,指出 GridTableStylesCollection 是否含有指定的 DataGridTableStyle

Contains(String)

取得值,指出 GridTableStylesCollection 是否含有依名稱指定的 DataGridTableStyle

CopyTo(Array, Int32)

將目前一維 Array 的所有元素複製到指定的一維 Array (從指定的目的 Array 索引開始)。

(繼承來源 BaseCollection)
CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetEnumerator()

取得允許逐一查看集合之成員的物件。

(繼承來源 BaseCollection)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs)

引發 CollectionChanged 事件。

Remove(DataGridTableStyle)

移除指定的 DataGridTableStyle

RemoveAt(Int32)

移除指定索引處的 DataGridTableStyle

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

CollectionChanged

發生於集合變更時。

明確介面實作

ICollection.CopyTo(Array, Int32)

將集合複製至相容的一維 Array (從目標陣列的指定索引處開始)。

ICollection.Count

取得集合中的項目數目。

ICollection.IsSynchronized

取得值,這個值表示對 GridTableStylesCollection 的存取是否同步 (安全執行緒)。

ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。

IEnumerable.GetEnumerator()

傳回集合的列舉程式。

IList.Add(Object)

DataGridTableStyle 新增至這個集合中。

IList.Clear()

清除集合。

IList.Contains(Object)

判斷項目是否在集合中。

IList.IndexOf(Object)

傳回指定的物件在集合中第一個出現的符合項目之索引 (此索引以零為起始)。

IList.Insert(Int32, Object)

實作 Insert(Int32, Object) 方法。 永遠擲回 NotSupportedException

IList.IsFixedSize

取得值,這個值表示集合的大小是否是固定的。

IList.IsReadOnly

取得值,表示集合是否為唯讀。

IList.Item[Int32]

在指定的索引位置上取得或設定項目。

IList.Remove(Object)

移除指定的 DataGridTableStyle

IList.RemoveAt(Int32)

將具有指定索引的 DataGridColumnStyle 從集合中移除。

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱