DataGridTableStyle 類別

定義

代表 DataGrid 控制項在執行階段中繪製的資料表。

public ref class DataGridTableStyle : System::ComponentModel::Component, System::Windows::Forms::IDataGridEditingService
public class DataGridTableStyle : System.ComponentModel.Component, System.Windows.Forms.IDataGridEditingService
type DataGridTableStyle = class
    inherit Component
    interface IDataGridEditingService
Public Class DataGridTableStyle
Inherits Component
Implements IDataGridEditingService
繼承
DataGridTableStyle
實作

範例

下列程式碼範例會建立兩 DataGridTableStyle 個 實例,並將 每個 物件的 設定 MappingNameTableNameDataTable 中的 DataSet 。 然後,此範例會將 物件加入 DataGridColumnStyleGridColumnStylesCollection 至每個 DataGridTableStyle 的 。 如需執行的範例,請參閱範例 System.Windows.Forms.DataGrid

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.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);
   }
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

備註

控制項 System.Windows.Forms.DataGrid 會以方格的形式顯示資料。 DataGridTableStyle是只代表繪製格線的類別。 此方格不應與 DataTable 類別混淆,這是方格的可能資料來源。 相反地, DataGridTableStyle 會嚴格表示在控制項中繪製的格線。 因此,您可以透過 DataGridTableStyle 來控制每個 DataTable 格線的外觀。 若要指定 DataGridTableStyle 從特定 DataTable 顯示資料時所使用的 ,請將 設定 MappingNameTableNameDataTable

透過 GridTableStylesCollection 屬性擷取的 TableStyles 包含 控制項使用 System.Windows.Forms.DataGrid 的所有 DataGridTableStyle 物件。 集合可以包含您所需的物件數目 DataGridTableStyle ,但 MappingName 每個 物件的 必須是唯一的。 在執行時間,這可讓您根據使用者的喜好設定,以不同的 DataGridTableStyle 資料取代相同的資料。 若要這樣做:

  1. GridTableStylesCollection 物件填入 DataGridTableStyleDataGridTableStyle如果 存在於 的 屬性值等於 DataTable 物件的 TableName 屬性中 GridTableStylesCollectionMappingName ,則會 DataTable 以這個 DataGridTableStyle 顯示 。 如果不存在 DataGridTableStyle 具有相符 MappingName 的 ,則會 DataTable 以資料格資料表的預設樣式顯示 。

  2. 需要不同的格線樣式時,請使用 Item 屬性來選取適當的 DataGridTableStyle (將 傳遞 TableNameItem[] 屬性) ,並將傳回物件的 設定 MappingName 為新的值。

  3. Item[]使用 屬性選取所需的 DataGridTableStyle ,並將其設定 MappingNameTableNameDataTable

警告

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

若要判斷目前顯示的專案 DataGridTableStyle ,請使用 DataSourceSystem.Windows.Forms.DataGridDataMember 屬性傳回 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如果 包含 DataTable 透過 DataRelation 物件相關的物件,且目前顯示的 DataTable 是子資料工作表,則會 DataMember 以 TableName.RelationName (形式傳回字串,最簡單的情況) 。 DataTable如果 在階層中進一步向下,字串將包含父資料表的名稱,後面接著達到資料表層級所需的必要 RelationName 值。 例如,假設階層式關聯性中的三 DataTable 個物件,名為 (由上至下) RegionsCustomersOrders ,以及名為 RegionsToCustomersCustomersToOrders 的兩 DataRelation 個物件,屬性 DataMember 會傳回 「Regions.RegionsToCustomers.CustomersToOrders」。 不過, MappingName 接著會是 「Orders」。

物件的集合 DataGridTableStyle 會透過 TableStylesSystem.Windows.Forms.DataGrid 屬性傳回。

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 系結至 ArrayList 。 的一項功能 ArrayList 是它可以包含多個類型的物件,但 DataGrid 只有在清單中的所有專案都與第一個專案的類型相同時,才能系結至這類清單。 這表示所有物件都必須是相同的類型,或者它們必須繼承自與清單中第一個專案相同的類別。 例如,如果清單中的第一個專案是 Control ,則第二個專案可能是 TextBox 繼承自 Control) (。 如果另一方面,第一個專案是 ,則第二個 TextBox 物件不能是 Control 。 此外,在 ArrayList 系結時,必須有其中的專案,而且 中的 DataGridTableStyle 物件必須包含公用屬性。 空白 ArrayList 會導致空的方格。 當系結至 ArrayList 時,請將 的 DataGridTableStyle 設定 MappingName 為 「ArrayList」 (類型名稱) 。

建構函式

DataGridTableStyle()

初始化 DataGridTableStyle 類別的新執行個體。

DataGridTableStyle(Boolean)

使用指定值初始化 DataGridTableStyle 類別的新執行個體,以判斷方格資料表是否為預設樣式。

DataGridTableStyle(CurrencyManager)

使用指定的 DataGridTableStyle 初始化 CurrencyManager 類別的新執行個體。

欄位

DefaultTableStyle

取得預設的資料表樣式。

屬性

AllowSorting

表示當使用這個 DataGridTableStyle 時,是否允許在方格資料表上進行排序。

AlternatingBackColor

取得或設定方格奇數資料列的背景色彩。

BackColor

取得或設定方格其偶數資料列的背景色彩。

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
ColumnHeadersVisible

取得或設定值,表示資料行行首是否為可見。

Container

取得包含 IContainerComponent

(繼承來源 Component)
DataGrid

取得或設定繪製資料表的 DataGrid 控制項。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
ForeColor

取得或設定方格資列表的前景色彩。

GridColumnStyles

取得為這個資料表所繪製的資料行集合。

GridLineColor

取得或設定格線色彩。

GridLineStyle

取得或設定格線樣式。

HeaderBackColor

取得或設定標頭的背景色彩。

HeaderFont

取得或設定標頭標題所使用的字型。

HeaderForeColor

取得或設定標頭的前景色彩。

LinkColor

取得或設定連結文字色彩。

LinkHoverColor

取得或設定停留在連結文字上時所顯示的色彩。

MappingName

取得或設定用來將這個資料表對應至指定資料來源的名稱。

PreferredColumnWidth

取得或設定當顯示新方格時,用來建立資料行的寬度。

PreferredRowHeight

取得或設定當顯示新方格時,用來建立資料列的高度。

ReadOnly

取得或設定值,表示是否可編輯資料行。

RowHeadersVisible

取得或設定值,表示資料列標頭是否為可見。

RowHeaderWidth

取得或設定資料列標頭的寬度。

SelectionBackColor

取得或設定選取儲存格的背景色彩。

SelectionForeColor

取得或設定選取儲存格的前景色彩。

Site

取得或設定 ComponentISite

(繼承來源 Component)

方法

BeginEdit(DataGridColumnStyle, Int32)

要求編輯作業。

CreateGridColumn(PropertyDescriptor)

使用指定的屬性描述項,建立 DataGridColumnStyle

CreateGridColumn(PropertyDescriptor, Boolean)

使用指定的屬性描述項,建立 DataGridColumnStyle。 指定 DataGridColumnStyle 是否為預設的資料行樣式。

CreateObjRef(Type)

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

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

處置 (Dispose) DataGridTableStyle 所使用的資源 (除了記憶體之外)。

EndEdit(DataGridColumnStyle, Int32, Boolean)

要求結束編輯作業。

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

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

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

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

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

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

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

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

(繼承來源 MarshalByRefObject)
OnAllowSortingChanged(EventArgs)

引發 AllowSortingChanged 事件。

OnAlternatingBackColorChanged(EventArgs)

引發 AlternatingBackColorChanged 事件。

OnBackColorChanged(EventArgs)

引發 BackColorChanged 事件。

OnColumnHeadersVisibleChanged(EventArgs)

引發 ColumnHeadersVisibleChanged 事件。

OnForeColorChanged(EventArgs)

引發 ForeColorChanged 事件。

OnGridLineColorChanged(EventArgs)

引發 GridLineColorChanged 事件。

OnGridLineStyleChanged(EventArgs)

引發 GridLineStyleChanged 事件。

OnHeaderBackColorChanged(EventArgs)

引發 HeaderBackColorChanged 事件。

OnHeaderFontChanged(EventArgs)

引發 HeaderFontChanged 事件。

OnHeaderForeColorChanged(EventArgs)

引發 HeaderForeColorChanged 事件。

OnLinkColorChanged(EventArgs)

引發 LinkColorChanged 事件。

OnLinkHoverColorChanged(EventArgs)

引發 LinkHoverColorChanged 事件。

OnMappingNameChanged(EventArgs)

引發 MappingNameChanged 事件。

OnPreferredColumnWidthChanged(EventArgs)

引發 PreferredColumnWidthChanged 事件。

OnPreferredRowHeightChanged(EventArgs)

引發 PreferredRowHeightChanged 事件。

OnReadOnlyChanged(EventArgs)

引發 ReadOnlyChanged 事件。

OnRowHeadersVisibleChanged(EventArgs)

引發 RowHeadersVisibleChanged 事件。

OnRowHeaderWidthChanged(EventArgs)

引發 RowHeaderWidthChanged 事件。

OnSelectionBackColorChanged(EventArgs)

引發 SelectionBackColorChanged 事件。

OnSelectionForeColorChanged(EventArgs)

引發 SelectionForeColorChanged 事件。

ResetAlternatingBackColor()

重設 AlternatingBackColor 屬性為其預設值。

ResetBackColor()

重設 BackColor 屬性為其預設值。

ResetForeColor()

重設 ForeColor 屬性為其預設值。

ResetGridLineColor()

重設 GridLineColor 屬性為其預設值。

ResetHeaderBackColor()

重設 HeaderBackColor 屬性為其預設值。

ResetHeaderFont()

重設 HeaderFont 屬性為其預設值。

ResetHeaderForeColor()

重設 HeaderForeColor 屬性為其預設值。

ResetLinkColor()

重設 LinkColor 屬性為其預設值。

ResetLinkHoverColor()

重設 LinkHoverColor 屬性為其預設值。

ResetSelectionBackColor()

重設 SelectionBackColor 屬性為其預設值。

ResetSelectionForeColor()

重設 SelectionForeColor 屬性為其預設值。

ShouldSerializeAlternatingBackColor()

指示是否應該保存 AlternatingBackColor 屬性。

ShouldSerializeBackColor()

指示是否應該保存 BackColor 屬性。

ShouldSerializeForeColor()

指示是否應該保存 ForeColor 屬性。

ShouldSerializeGridLineColor()

指示是否應該保存 GridLineColor 屬性。

ShouldSerializeHeaderBackColor()

指示是否應該保存 HeaderBackColor 屬性。

ShouldSerializeHeaderForeColor()

指示是否應該保存 HeaderForeColor 屬性。

ShouldSerializeLinkColor()

指示是否應該保存 LinkColor 屬性。

ShouldSerializeLinkHoverColor()

指示是否應該保存 LinkHoverColor 屬性。

ShouldSerializePreferredRowHeight()

指示是否應該保存 PreferredRowHeight 屬性。

ShouldSerializeSelectionBackColor()

指示是否應該保存 SelectionBackColor 屬性。

ShouldSerializeSelectionForeColor()

指示是否應該保存 SelectionForeColor 屬性。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)

事件

AllowSortingChanged

發生在 AllowSorting 屬性值變更時。

AlternatingBackColorChanged

發生於 AlternatingBackColor 值變更時。

BackColorChanged

發生於 BackColor 值變更時。

ColumnHeadersVisibleChanged

發生於 ColumnHeadersVisible 值變更時。

Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)
ForeColorChanged

發生於 ForeColor 值變更時。

GridLineColorChanged

發生於 GridLineColor 值變更時。

GridLineStyleChanged

發生於 GridLineStyle 值變更時。

HeaderBackColorChanged

發生於 HeaderBackColor 值變更時。

HeaderFontChanged

發生於 HeaderFont 值變更時。

HeaderForeColorChanged

發生於 HeaderForeColor 值變更時。

LinkColorChanged

發生於 LinkColor 值變更時。

LinkHoverColorChanged

發生於 LinkHoverColor 值變更時。

MappingNameChanged

發生於 MappingName 值變更時。

PreferredColumnWidthChanged

發生在 PreferredColumnWidth 屬性值變更時。

PreferredRowHeightChanged

發生於 PreferredRowHeight 值變更時。

ReadOnlyChanged

發生於 ReadOnly 值變更時。

RowHeadersVisibleChanged

發生於 RowHeadersVisible 值變更時。

RowHeaderWidthChanged

發生於 RowHeaderWidth 值變更時。

SelectionBackColorChanged

發生於 SelectionBackColor 值變更時。

SelectionForeColorChanged

發生於 SelectionForeColor 值變更時。

適用於

另請參閱