DataGrid.TableStyles 属性

获取网格的 DataGridTableStyle 对象的集合。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
<LocalizableAttribute(True)> _
Public ReadOnly Property TableStyles As GridTableStylesCollection
用法
Dim instance As DataGrid
Dim value As GridTableStylesCollection

value = instance.TableStyles
[LocalizableAttribute(true)] 
public GridTableStylesCollection TableStyles { get; }
[LocalizableAttribute(true)] 
public:
property GridTableStylesCollection^ TableStyles {
    GridTableStylesCollection^ get ();
}
/** @property */
public GridTableStylesCollection get_TableStyles ()
public function get TableStyles () : GridTableStylesCollection

属性值

一个 GridTableStylesCollection,它代表 DataGridTableStyle 对象的集合。

备注

使用从 TableStyles 属性中检索到的 GridTableStylesCollection 创建 System.Windows.Forms.DataGrid 控件所显示的每个表的自定义视图。

默认情况下,TableStyles 属性所返回的集合不包含任何 DataGridTableStyle 对象。创建一组自定义视图:

  1. 创建 DataGridTableStyle

  2. 将网格表对象的 MappingName 设置为 DataTableTableName

  3. 对于想显示的每个网格列,均要向 GridColumnStyles 属性所返回的 GridColumnStylesCollection 添加一个 DataGridColumnStyle 对象。

  4. 将每个 DataGridColumnStyleMappingName 设置为一个 DataColumnColumnName

  5. TableStyles 属性所返回的集合添加 DataGridTableStyle 对象。

Caution note警告

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

示例

下面的代码示例为 DataSet 中的每个 DataTable 创建一个 DataGridTableStyle,并将 DataGridTableStyleMappingName 设置为 DataTableTableNameDataGridTableStyle 随后被添加到 TableStyles 属性所返回的 GridTableStylesCollection 中。该示例还输出 GridTableStylesCollection 中每个 DataGridTableStyleGridColumnStyles 属性所返回 GridColumnStylesCollection 中每个 DataGridColumnStyleMappingName

Private Sub AddTables(myDataGrid As DataGrid, _
myDataSet As DataSet )
    Dim t As DataTable 
    For Each t in myDataSet.Tables
       Dim myGridTableStyle As DataGridTableStyle  = new _
       DataGridTableStyle()
          myGridTableStyle.MappingName = t.TableName
          myDataGrid.TableStyles.Add(myGridTableStyle)
       ' Note that DataGridColumnStyle objects will
       ' be created automatically for the first DataGridTableStyle
       ' when you add it to the GridTableStylesCollection.*/
    Next
 End Sub
 Private Sub PrintGridStyleInfo(myDataGrid As DataGrid )
    Dim myGridStyle As DataGridTableStyle
    Dim myColumnStyle As DataGridColumnStyle
    
    for each myGridStyle in _
    myDataGrid.TableStyles
       Console.WriteLine(myGridStyle.MappingName)
       for each myColumnStyle in myGridStyle.GridColumnStyles
         Console.WriteLine(myColumnStyle.MappingName)
       Next
    Next
 End Sub    
private void AddTables(DataGrid myDataGrid, DataSet myDataSet){
   foreach(DataTable t in myDataSet.Tables){
      DataGridTableStyle myGridTableStyle = new 
      DataGridTableStyle();
      myGridTableStyle.MappingName = t.TableName;
      myDataGrid.TableStyles.Add(myGridTableStyle);

      /* Note that DataGridColumnStyle objects will
      be created automatically for the first DataGridTableStyle
      when you add it to the GridTableStylesCollection.*/
   }
}
private void PrintGridStyleInfo(DataGrid myDataGrid){
   /* Print the MappingName of each DataGridTableStyle,
   and the MappingName of each DataGridColumnStyle. */
   foreach(DataGridTableStyle myGridStyle in 
   myDataGrid.TableStyles){
   Console.WriteLine(myGridStyle.MappingName);
   foreach(DataGridColumnStyle myColumnStyle in 
      myGridStyle.GridColumnStyles){
     Console.WriteLine(myColumnStyle.MappingName);
      }
   }
}
 
private:
   void AddTables( DataGrid^ myDataGrid, DataSet^ myDataSet )
   {
      for each ( DataTable^ t in myDataSet->Tables )
      {
         DataGridTableStyle^ myGridTableStyle =
            gcnew DataGridTableStyle;
         myGridTableStyle->MappingName = t->TableName;
         myDataGrid->TableStyles->Add( myGridTableStyle );
         
         /* Note that DataGridColumnStyle objects will
            be created automatically for the first DataGridTableStyle
            when you add it to the GridTableStylesCollection.*/
      }
   }

   void PrintGridStyleInfo( DataGrid^ myDataGrid )
   {
      /* Print the MappingName of each DataGridTableStyle,
         and the MappingName of each DataGridColumnStyle. */
      for each ( DataGridTableStyle^ myGridStyle in
         myDataGrid->TableStyles )
      {
         Console::WriteLine( myGridStyle->MappingName );
         for each ( DataGridColumnStyle^ myColumnStyle in
            myGridStyle->GridColumnStyles )
         {
            Console::WriteLine( myColumnStyle->MappingName );
         }
      }
   }
private void AddTables(DataGrid myDataGrid, DataSet myDataSet)
{
    DataTable t;
    for (int iCtr = 0; iCtr < myDataSet.get_Tables().get_Count(); iCtr++) {
        t = myDataSet.get_Tables().get_Item(iCtr);
        DataGridTableStyle myGridTableStyle = new DataGridTableStyle();
        myGridTableStyle.set_MappingName(t.get_TableName());
        myDataGrid.get_TableStyles().Add(myGridTableStyle);
    }
} //AddTables

/*  Note that DataGridColumnStyle objects will
    be created automatically for the first DataGridTableStyle
    when you add it to the GridTableStylesCollection.
 */
private void PrintGridStyleInfo(DataGrid myDataGrid)
{
    DataGridTableStyle myGridStyle;
    for (int iCtr = 0; iCtr < myDataGrid.get_TableStyles().get_Count(); 
        iCtr++) {
        /*  Print the MappingName of each DataGridTableStyle,
            and the MappingName of each DataGridColumnStyle. 
         */
        myGridStyle = myDataGrid.get_TableStyles().get_Item(iCtr);
        Console.WriteLine(myGridStyle.get_MappingName());
        DataGridColumnStyle myColumnStyle;
        for (int iCtr1 = 0; 
            iCtr1 < myGridStyle.get_GridColumnStyles().get_Count();
            iCtr1++) {
            myColumnStyle = myGridStyle.get_GridColumnStyles().
                get_Item(iCtr1);
            Console.WriteLine(myColumnStyle.get_MappingName());
        }
    }
} //PrintGridStyleInfo

平台

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

请参见

参考

DataGrid 类
DataGrid 成员
System.Windows.Forms 命名空间
DataColumn
DataTable
DataGridTableStyle
GridTableStylesCollection