GridTableStylesCollection Class

Definition

表示 DataGrid 控件中 DataGridTableStyle 对象的集合。Represents a collection of DataGridTableStyle objects in the DataGrid control.

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
type GridTableStylesCollection = class
    inherit BaseCollection
    interface IList
    interface ICollection
    interface IEnumerable
Public Class GridTableStylesCollection
Inherits BaseCollection
Implements IList
Inheritance
GridTableStylesCollection
Attributes
Implements

Examples

下面的代码示例创建两个 DataGridTableStyle 对象,并将每个对象添加到 DataGrid 控件的 TableStyles 属性返回的 GridTableStylesCollection 中。The following code example creates two DataGridTableStyle objects and adds each to the GridTableStylesCollection returned by the TableStyles property of a DataGrid control.

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

Remarks

GridTableStylesCollection 包含 DataGridTableStyle 对象,这些对象允许 DataGrid 控件为 DataSet中的每个 DataTable 显示自定义网格样式。The GridTableStylesCollection contains DataGridTableStyle objects that allows the DataGrid control to display a customized grid style for each DataTable in a DataSet.

DataGrid 控件上,TableStyles 属性返回 GridTableStylesCollectionOn the DataGrid control, the TableStyles property returns the GridTableStylesCollection.

默认情况下,GridTableStylesCollection 不包含任何 DataGridTableStyle 对象。By default, the GridTableStylesCollection does not contain any DataGridTableStyle objects. 相反,DataGrid 使用颜色、宽度和格式设置的默认设置显示每个表。Instead, the DataGrid displays each table using default settings for color, width, and formatting. 显示每个表的所有列。All columns of each table are displayed. 当向集合添加 DataGridTableStyle 时,DataGrid 将使用 MappingName 来确定为网格提供数据的对象。When a DataGridTableStyle is added to the collection, the DataGrid uses the MappingName to determine which object supplies the data for the grid. 例如,如果数据源是一个包含三个 DataTable 对象的 DataSet,则 MappingName 必须与其中一个对象的 TableName 匹配。For example, if the data source is a DataSet that contains three DataTable objects, the MappingName must match the TableName of one of the objects. 如果 MappingName 与任何 TableName 值都不匹配,则默认设置将用于显示每个表的数据,DataGridTableStyle 设置将被忽略。If the MappingName does not match any of the TableName values, the default settings will be used to display data for each table, and the DataGridTableStyle settings will be ignored.

Caution

在将 DataGridTableStyle 对象添加到 GridTableStylesCollection之前,始终创建 DataGridColumnStyle 对象并将其添加到 GridColumnStylesCollection 中。Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection. 将具有有效 MappingName 值的空 DataGridTableStyle 添加到集合时,会自动为你生成 DataGridColumnStyle 对象。When you add an empty DataGridTableStyle with a valid MappingName value to the collection, DataGridColumnStyle objects are automatically generated for you. 因此,如果尝试将具有重复 MappingName 值的新 DataGridColumnStyle 对象添加到 GridColumnStylesCollection,则会引发异常。Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection. 或者,使用 Clear 方法清除 GridColumnStylesCollectionAlternatively, clear the GridColumnStylesCollection using the Clear method.

Properties

Count

获取集合中的总元素数。Gets the total number of elements in the collection.

(Inherited from BaseCollection)
IsReadOnly

获取一个值,该值指示集合是否是只读的。Gets a value indicating whether the collection is read-only.

(Inherited from BaseCollection)
IsSynchronized

获取一个值,该值指示对 ICollection 的访问是否同步。Gets a value indicating whether access to the ICollection is synchronized.

(Inherited from BaseCollection)
Item[Int32]

获取索引指定的 DataGridTableStyleGets the DataGridTableStyle specified by index.

Item[String]

获取具有指定名称的 DataGridTableStyleGets the DataGridTableStyle with the specified name.

List

获取基础列表。Gets the underlying list.

SyncRoot

获取用于同步对 BaseCollection 的访问的对象。Gets an object that can be used to synchronize access to the BaseCollection.

(Inherited from BaseCollection)

Methods

Add(DataGridTableStyle)

DataGridTableStyle 添加到此集合。Adds a DataGridTableStyle to this collection.

AddRange(DataGridTableStyle[])

将一个表样式数组添加到集合中。Adds an array of table styles to the collection.

Clear()

清除集合。Clears the collection.

Contains(DataGridTableStyle)

获取一个值,该值指示 GridTableStylesCollection 是否包含指定的 DataGridTableStyleGets a value indicating whether the GridTableStylesCollection contains the specified DataGridTableStyle.

Contains(String)

获取一个值,该值指示 GridTableStylesCollection 是否包含由名称指定的 DataGridTableStyleGets a value indicating whether the GridTableStylesCollection contains the DataGridTableStyle specified by name.

CopyTo(Array, Int32)

将当前一维 Array 的所有元素复制到指定的一维 Array 中(从指定的目标 Array 索引开始)。Copies all the elements of the current one-dimensional Array to the specified one-dimensional Array starting at the specified destination Array index.

(Inherited from BaseCollection)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

获取能够循环访问集合成员的对象。Gets the object that enables iterating through the members of the collection.

(Inherited from BaseCollection)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
OnCollectionChanged(CollectionChangeEventArgs)

引发 CollectionChanged 事件。Raises the CollectionChanged event.

Remove(DataGridTableStyle)

删除指定的 DataGridTableStyleRemoves the specified DataGridTableStyle.

RemoveAt(Int32)

移除指定索引处的 DataGridTableStyleRemoves a DataGridTableStyle at the specified index.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Events

CollectionChanged

在集合更改后发生。Occurs when the collection has changed.

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32)

从目标数组的指定索引处开始,将集合复制到兼容的一维 ArrayCopies the collection to a compatible one-dimensional Array, starting at the specified index of the target array.

ICollection.Count

获取集合中的项数。Gets the number of items in the collection.

ICollection.IsSynchronized

获取一个值,该值指示是否同步对 GridTableStylesCollection 的访问(确保线程安全)。Gets a value indicating whether access to the GridTableStylesCollection is synchronized (thread safe).

ICollection.SyncRoot

获取可用于同步对集合的访问的对象。Gets an object that can be used to synchronize access to the collection.

IEnumerable.GetEnumerator()

返回集合的枚举数。Returns an enumerator for the collection.

IList.Add(Object)

DataGridTableStyle 添加到此集合。Adds a DataGridTableStyle to this collection.

IList.Clear()

清除集合。Clears the collection.

IList.Contains(Object)

确定某元素是否在集合中。Determines whether an element is in the collection.

IList.IndexOf(Object)

返回指定对象在集合中的第一个匹配项的从零开始的索引。Returns the zero-based index of the first occurrence of the specified object in the collection.

IList.Insert(Int32, Object)

实现 Insert(Int32, Object) 方法。Implements the Insert(Int32, Object) method. 总是引发 NotSupportedExceptionAlways throws NotSupportedException.

IList.IsFixedSize

获取一个值,该值指示集合是否具有固定大小。Gets a value indicating whether the collection has a fixed size.

IList.IsReadOnly

获取一个值,该值指示集合是否是只读的。Gets a value indicating whether the collection is read-only.

IList.Item[Int32]

获取或设置指定索引处的元素。Gets or sets the element at the specified index.

IList.Remove(Object)

删除指定的 DataGridTableStyleRemoves the specified DataGridTableStyle.

IList.RemoveAt(Int32)

从集合中移除具有指定索引的 DataGridColumnStyleRemoves the DataGridColumnStyle with the specified index from the collection.

Extension Methods

Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

Applies to

See also