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 设置为某 DataTableTableName

通过 TableStyles 属性检索的 GridTableStylesCollection 包含所有由 System.Windows.Forms.DataGrid 控件使用的 DataGridTableStyle 对象。该集合可以包含所需要的、任意多个 DataGridTableStyle 对象,但是,它们的 MappingName 必须唯一。这使您可以在运行时根据用户的首选项,用另一种 DataGridTableStyle 替换相同数据。若要进行此操作:

  1. DataGridTableStyle 对象填充 GridTableStylesCollection。如果在 GridTableStylesCollection 中存在一个 DataGridTableStyle,其 MappingName 属性值等于相应 DataTable 对象的 TableName 属性,则使用此 DataGridTableStyle 显示该 DataTable。如果没有匹配 MappingNameDataGridTableStyle,则使用数据网格表的默认样式显示 DataTable

  2. 当需要不同的网格样式时,可使用 Item 属性选择适当的 DataGridTableStyle(向 Item 属性传递 TableName),并将所返回对象的 MappingName 设置为一个新值。

  3. 使用 Item 属性选择所需的 DataGridTableStyle,将其 MappingName 设置为相应 DataTableTableName

警告

在向 GridTableStylesCollection 添加 DataGridTableStyle 对象之前,始终应先创建 DataGridColumnStyle 对象并将其添加到 GridColumnStylesCollection。当向集合添加具有有效的 MappingName 值的空 DataGridTableStyle 时,将自动生成 DataGridColumnStyle 对象。因此,如果试图向 GridColumnStylesCollection 添加带有重复 MappingName 值的 DataGridColumnStyle 新对象时,将引发异常。

若要确定当前所显示的 DataGridTableStyle,请使用 System.Windows.Forms.DataGridDataSourceDataMember 属性来返回一个 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 值。例如,假定分层关系中有三个 DataTable 对象,从上到下分别命名为 RegionsCustomersOrders,还有两个 DataRelation 对象,分别命名为 RegionsToCustomersCustomersToOrders,则 DataMember 属性将返回“Regions.RegionsToCustomers.CustomersToOrders”。但是,MappingName 将为“Orders”。

DataGridTableStyle 对象的集合通过 System.Windows.Forms.DataGridTableStyles 属性返回。

当显示某 DataGridTableStyle 时,该 DataGridTableStyle 的设置将重写 System.Windows.Forms.DataGrid 控件的设置。如果未设置特定的 DataGridTableStyle 属性的值,则将改用 System.Windows.Forms.DataGrid 控件的值。下面的列表显示 DataGridColumnStyle 属性,可设置这些属性来重写 System.Windows.Forms.DataGrid 控件属性:

若要将 DataGrid 绑定到对象的强类型数组,该对象类型必须包含公共属性。若要创建一个显示此数组的 DataGridTableStyle,请将 DataGridTableStyle.MappingName 属性设置为 typename,其中 typename 由该对象类型的名称替换。另外请注意,MappingName 属性区分大小写;类型名称必须完全匹配。有关示例,请参见 MappingName 属性。

也可以将 DataGrid 绑定到 ArrayListArrayList 的一个功能是它可以包含多种类型的对象,但当列表中的所有项与第一项具有相同的类型时,DataGrid 只能绑定到这类列表。这意味着所有的对象必须是同一种类型,或者必须从与列表中第一项相同的类继承。例如,如果列表中的第一项为 Control,则第二项可能为 TextBox(它从 Control 继承)。另一方面,如果第一项为 TextBox,则第二个对象就不可能是 Control。此外,当绑定 ArrayList 时,其中必须具有项,并且 DataGridTableStyle 中的对象必须包含公共属性。空 ArrayList 会导致空网格。当绑定到 ArrayList 时,请将 DataGridTableStyleMappingName 设置为“ArrayList”(类型名)。

示例

下面的代码示例创建两个 DataGridTableStyle 实例,并将每个对象的 MappingName 设置为 DataSet 中一个 DataTableTableName。然后,该示例将 DataGridColumnStyle 对象添加到每个 DataGridTableStyleGridColumnStylesCollection 中。有关可运行的示例,请参见 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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

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 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DataGridTableStyle 成员
System.Windows.Forms 命名空间
DataGrid 类
DataGridColumnStyle 类
GridColumnStylesCollection
GridTableStylesCollection