DataGridTableStyle Classe
TOC
Recolher sumário
Expandir sumário
Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações.
Tradução
Original

DataGridTableStyle Classe

Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (em System.Windows.Forms. dll)

public class DataGridTableStyle : Component, 
	IDataGridEditingService

The System.Windows.Forms.DataGrid control displays data in the form of a grid.The DataGridTableStyle is a class that represents the drawn grid only.This grid should not be confused with the DataTable class, which is a possible source of data for the grid.Instead, the DataGridTableStyle strictly represents the grid as it is painted in the control.Therefore, through the DataGridTableStyle you can control the appearance of the grid for each DataTable.To specify which DataGridTableStyle is used when displaying data from a particular DataTable, set the MappingName to the TableName of a DataTable.

The GridTableStylesCollection retrieved through the TableStyles property contains all the DataGridTableStyle objects used by a System.Windows.Forms.DataGrid control.The collection can contain as many DataGridTableStyle objects as you need, however the MappingName of each must be unique.At run time, this allows you to substitute a different DataGridTableStyle for the same data, depending on the user's preference.Para fazer isso:

  1. Populate the GridTableStylesCollection with DataGridTableStyle objects.If a DataGridTableStyle exists in the GridTableStylesCollection whose MappingName property value equals the DataTable object's TableName property, the DataTable is displayed with this DataGridTableStyle.If no DataGridTableStyle exists with a matching MappingName, the DataTable is displayed with the default style for data grid tables.

  2. When a different grid style is needed, use the Item property to select the appropriate DataGridTableStyle (pass the TableName to the Item property) and set the MappingName of the returned object to a new value.

  3. Use the Item property to select the desired DataGridTableStyle, and set its MappingName to the TableName of the DataTable.

Observação de cautelaCuidado:

Always create DataGridColumnStyle objects and add them to the GridColumnStylesCollection before adding DataGridTableStyle objects to the GridTableStylesCollection.When you add an empty DataGridTableStyle with a valid MappingName value to the collection, DataGridColumnStyle objects are automatically generated for you.Consequently, an exception will be thrown if you try to add new DataGridColumnStyle objects with duplicate MappingName values to the GridColumnStylesCollection.

To determine which DataGridTableStyle is currently displayed, use the DataSource and DataMember properties of the System.Windows.Forms.DataGrid to return a CurrencyManager.If the data source implements the ITypedList interface, you can use the GetListName method to return the MappingName of the current table.Isso é mostrado no código a C# abaixo:

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

If the DataSet contains DataTable objects related through DataRelation objects, and the currently displayed DataTable is a child table, the DataMember will return a string in the form of TableName.RelationName (in the simplest case).If the DataTable is further down in the hierarchy, the string will consist of the parent table's name followed by the necessary RelationName values required to reach the table's level.For example, imagine three DataTable objects in a hierarchical relationship named (top to bottom) Regions, Customers, and Orders, and two DataRelation objects named RegionsToCustomers and CustomersToOrders, the DataMember property will return "Regions.RegionsToCustomers.CustomersToOrders".However, the MappingName will then be "Orders".

The collection of DataGridTableStyle objects is returned through the TableStyles property of the System.Windows.Forms.DataGrid.

When a DataGridTableStyle is displayed, the settings for the DataGridTableStyle will override the settings for the System.Windows.Forms.DataGrid control.If a value is not set for a particular DataGridTableStyle property, the System.Windows.Forms.DataGrid control's value will be used instead.The following list shows the DataGridColumnStyle properties that can be set to override System.Windows.Forms.DataGrid control properties:

Para vincular o DataGrid a uma matriz de objetos fortemente tipada, o tipo de objeto deve conter propriedades públicas.Para criar um DataGridTableStyle que exibe a matriz, defina a propriedade DataGridTableStyle.MappingName para typename, sendo typename substituído pelo nome do tipo objeto.Observe também que a propriedade MappingName diferencia maiúsculas de minúsculas; o nome do tipo deve ter correspondência exata.Consulte a propriedade MappingName para um exemplo.

Você também pode vincular o DataGrid a um ArrayList.Uma característica do ArrayList é que ele pode conter objetos de múltiplos tipos, mas o DataGrid só pode ser vinculado a uma lista quando todos os itens da lista são do mesmo tipo do primeiro item.Isso significa que todos os objetos devem ou ser do mesmo tipo, ou herdar da mesma classe conforme o primeiro item na lista.Por exemplo, se o primeiro item de uma lista for um Control, o segundo item poderia ser um TextBox (que herda de Control).Se, por outro lado, o primeiro item é um TextBox, o segundo objeto não pode ser um Control.Further, the ArrayList must have items in it when it is bound and the objects in the DataGridTableStyle must contain public properties.Uma ArrayList vazia resultará em uma grade vazia.Quando vincular a um ArrayList, defina o MappingName do DataGridTableStyle para "ArrayList" (nome do tipo).

The following code example creates two DataGridTableStyle instances and sets the MappingName of each object to the TableName of a DataTable in a DataSet.The example then adds DataGridColumnStyle objects to the GridColumnStylesCollection of each DataGridTableStyle.For an example that runs, see the System.Windows.Forms.DataGrid example.

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 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

Quaisquer membros públicos estático (compartilhados na Visual Basic) desse tipo são Thread seguro. Não há garantia de que qualquer membro de instância seja isento de segmentos.

Contribuições da comunidade

ADICIONAR
Mostrar:
© 2016 Microsoft