DataGridTableStyle.MappingName Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit le nom utilisé pour mapper la table avec une source de données spécifique.
public:
property System::String ^ MappingName { System::String ^ get(); void set(System::String ^ value); };
public string MappingName { get; set; }
member this.MappingName : string with get, set
Public Property MappingName As String
Valeur de propriété
Nom utilisé pour mapper la grille avec une source de données spécifique.
Exemples
L’exemple de code suivant crée un tableau d’objets Widget
et lie un System.Windows.Forms.DataGrid contrôle au tableau. Le code crée ensuite un DataGridTableStyle et définit le MappingName sur le nom de la classe avec des crochets.
void BindToArray()
{
// Create an array of Machine objects (defined below).
array<Machine^>^ Machines = gcnew array<Machine^>(3);
Machine^ tempMachine;
tempMachine = gcnew Machine();
tempMachine->Model = "AAA";
tempMachine->Id = "A100";
tempMachine->Price = Convert::ToDecimal(3.80);
Machines[0] = tempMachine;
// The first Machine includes an array of Part objects.
Part^ p1 = gcnew Part();
p1->PartId = "PartX";
Part^ p2 = gcnew Part();
p2->PartId = "PartY";
// Note that the Machines.Parts property returns an ArrayList.
// Add the parts to the ArrayList using the AddRange method.
tempMachine->Parts->AddRange(gcnew array<Part^> {p1, p2});
tempMachine = gcnew Machine();
tempMachine->Model = "BBB";
tempMachine->Id = "B100";
tempMachine->Price = Convert::ToDecimal(1.52);
Machines[1] = tempMachine;
tempMachine = gcnew Machine();
tempMachine->Id = "CCC";
tempMachine->Model = "B100";
tempMachine->Price = Convert::ToDecimal(2.14);
Machines[2] = tempMachine;
bindedDataGrid->SetDataBinding(Machines, "");
CreateTableStyle();
}
void CreateTableStyle()
{
// Creates two DataGridTableStyle objects, one for the Machine
// array, and one for the Parts ArrayList.
DataGridTableStyle^ machineTable = gcnew DataGridTableStyle();
// Sets the MappingName to the class name plus brackets.
machineTable->MappingName = "Machine[]";
// Sets the AlternatingBackColor so you can see the difference.
machineTable->AlternatingBackColor =
System::Drawing::Color::LightBlue;
// Creates three column styles.
DataGridTextBoxColumn^ modelColumn = gcnew DataGridTextBoxColumn();
modelColumn->MappingName = "Model";
modelColumn->HeaderText = "Model";
DataGridTextBoxColumn^ idColumn = gcnew DataGridTextBoxColumn();
idColumn->MappingName = "Id";
idColumn->HeaderText = "Id";
DataGridTextBoxColumn^ priceColumn = gcnew DataGridTextBoxColumn();
priceColumn->MappingName = "Price";
priceColumn->HeaderText = "Price";
priceColumn->Format = "c";
// Adds the column styles to the grid table style.
machineTable->GridColumnStyles->Add(modelColumn);
machineTable->GridColumnStyles->Add(idColumn);
machineTable->GridColumnStyles->Add(priceColumn);
// Add the table style to the collection, but clear the
// collection first.
bindedDataGrid->TableStyles->Clear();
bindedDataGrid->TableStyles->Add(machineTable);
// Create another table style, one for the related data.
DataGridTableStyle^ partsTable = gcnew DataGridTableStyle();
// Set the MappingName to an ArrayList. Note that you need not
// include brackets.
partsTable->MappingName = "ArrayList";
DataGridTextBoxColumn^ partIdColumn =
gcnew DataGridTextBoxColumn();
partIdColumn->MappingName = "PartID";
partIdColumn->HeaderText = "Part ID";
partsTable->GridColumnStyles->Add(partIdColumn);
bindedDataGrid->TableStyles->Add(partsTable);
}
private:
ref class Machine
{
private:
String^ machineModel;
String^ machineID;
Decimal machinePrice;
// Use an ArrayList to create a related collection.
ArrayList^ machineParts;
public:
Machine()
{
machineParts = gcnew ArrayList;
}
property String^ Model
{
String^ get()
{
return machineModel;
}
void set(String^ value)
{
machineModel = value;
}
}
property String^ Id
{
String^ get()
{
return machineID;
}
void set(String^ value)
{
machineID = value;
}
}
property ArrayList^ Parts
{
ArrayList^ get()
{
return machineParts;
}
void set(ArrayList^ value)
{
machineParts = value;
}
}
property Decimal Price
{
Decimal get()
{
return machinePrice;
}
void set(Decimal value)
{
machinePrice = value;
}
}
};
private:
ref class Part
{
private:
String^ partId;
public:
property String^ PartId
{
String^ get()
{
return partId;
}
void set(String^ value)
{
partId = value;
}
}
};
private void BindToArray()
{
// Create an array of Machine objects (defined below).
Machine[] Machines = new Machine[3];
Machine tempMachine;
tempMachine= new Machine();
tempMachine.Model = "AAA";
tempMachine.Id= "A100";
tempMachine.Price= Convert.ToDecimal(3.80);
Machines[0]=tempMachine;
// The first Machine includes an array of Part objects.
Part p1 = new Part();
p1.PartId= "PartX";
Part p2 = new Part();
p2.PartId= "PartY";
// Note that the Machines.Parts property returns an ArrayList.
// Add the parts to the ArrayList using the AddRange method.
tempMachine.Parts.AddRange (new Part[]{p1, p2});;
tempMachine= new Machine();
tempMachine.Model = "BBB";
tempMachine.Id= "B100";
tempMachine.Price= Convert.ToDecimal(1.52);
Machines[1]=tempMachine;
tempMachine= new Machine();
tempMachine.Id= "CCC";
tempMachine.Model = "B100";
tempMachine.Price= Convert.ToDecimal(2.14);
Machines[2]=tempMachine;
dataGrid1.SetDataBinding(Machines, "");
CreateTableStyle();
}
private void CreateTableStyle()
{
// Creates two DataGridTableStyle objects, one for the Machine
// array, and one for the Parts ArrayList.
DataGridTableStyle MachineTable = new DataGridTableStyle();
// Sets the MappingName to the class name plus brackets.
MachineTable.MappingName= "Machine[]";
// Sets the AlternatingBackColor so you can see the difference.
MachineTable.AlternatingBackColor= System.Drawing.Color.LightBlue;
// Creates three column styles.
DataGridTextBoxColumn modelColumn = new DataGridTextBoxColumn();
modelColumn.MappingName= "Model";
modelColumn.HeaderText= "Model";
DataGridTextBoxColumn IdColumn = new DataGridTextBoxColumn();
IdColumn.MappingName= "Id";
IdColumn.HeaderText= "Id";
DataGridTextBoxColumn priceColumn = new DataGridTextBoxColumn();
priceColumn.MappingName= "Price";
priceColumn.HeaderText= "Price";
priceColumn.Format = "c";
// Adds the column styles to the grid table style.
MachineTable.GridColumnStyles.Add(modelColumn);
MachineTable.GridColumnStyles.Add(IdColumn);
MachineTable.GridColumnStyles.Add(priceColumn);
// Add the table style to the collection, but clear the
// collection first.
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(MachineTable);
// Create another table style, one for the related data.
DataGridTableStyle partsTable = new DataGridTableStyle();
// Set the MappingName to an ArrayList. Note that you need not
// include brackets.
partsTable.MappingName= "ArrayList";
DataGridTextBoxColumn partIdColumn = new DataGridTextBoxColumn();
partIdColumn.MappingName= "PartID";
partIdColumn.HeaderText = "Part ID";
partsTable.GridColumnStyles.Add(partIdColumn);
dataGrid1.TableStyles.Add(partsTable);
}
public class Machine
{
private string model;
private string id;
private decimal price;
// Use an ArrayList to create a related collection.
private ArrayList parts = new ArrayList();
public string Model
{
get{return model;}
set{model=value;}
}
public string Id
{
get{return id;}
set{id = value;}
}
public ArrayList Parts
{
get{return parts;}
set{parts = value;}
}
public decimal Price
{
get{return price;}
set{price = value;}
}
}
public class Part
{
private string partId;
public string PartId
{
get{return partId;}
set{partId = value;}
}
}
Sub BindToArray()
' Creates an array of Widget objects (defined below).
Dim Widgets(2) As Widget
Dim tempWidget As Widget
tempWidget = New Widget()
tempWidget.Model = "AAA"
tempWidget.Id = "A100"
tempWidget.Price = Convert.ToDecimal(3.8)
Widgets(0) = tempWidget
' The first Widget includes an array of Part objects.
Dim p1 As New Part()
p1.PartId = "PartX"
Dim p2 As New Part()
p2.PartId = "PartY"
' Note that the Widgets.Parts property returns an ArrayList.
' Add the parts to the ArrayList using the AddRange method.
tempWidget.Parts.AddRange(New Part() {p1, p2})
tempWidget = New Widget()
tempWidget.Model = "BBB"
tempWidget.Id = "B100"
tempWidget.Price = Convert.ToDecimal(1.52)
Widgets(1) = tempWidget
tempWidget = New Widget()
tempWidget.Id = "CCC"
tempWidget.Model = "B100"
tempWidget.Price = Convert.ToDecimal(2.14)
Widgets(2) = tempWidget
DataGrid1.SetDataBinding(Widgets, "")
CreateTableStyle()
End Sub
Private Sub CreateTableStyle()
' Creates two DataGridTableStyle objects, one for the Widget
' array, and one for the Parts ArrayList.
Dim widgetTable As New DataGridTableStyle()
' Sets the MappingName to the class name plus brackets.
widgetTable.MappingName = "Widget[]"
' Sets the AlternatingBackColor so you can see the difference.
widgetTable.AlternatingBackColor = System.Drawing.Color.LightBlue
' Creates three column styles.
Dim modelColumn As New DataGridTextBoxColumn()
modelColumn.MappingName = "Model"
modelColumn.HeaderText = "Model"
Dim IdColumn As New DataGridTextBoxColumn()
IdColumn.MappingName = "Id"
IdColumn.HeaderText = "Id"
Dim priceColumn As New DataGridTextBoxColumn()
priceColumn.MappingName = "Price"
priceColumn.HeaderText = "Price"
priceColumn.Format = "c"
' Adds the column styles to the grid table style.
widgetTable.GridColumnStyles.Add(modelColumn)
widgetTable.GridColumnStyles.Add(IdColumn)
widgetTable.GridColumnStyles.Add(priceColumn)
' Add the table style to the collection, but clear the
' collection first.
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(widgetTable)
' Create another table style, one for the related data.
Dim partsTable As New DataGridTableStyle()
' Set the MappingName to an ArrayList. Note that you need not
' include brackets.
partsTable.MappingName = "ArrayList"
Dim partIdColumn As New DataGridTextBoxColumn()
partIdColumn.MappingName = "PartID"
partIdColumn.HeaderText = "Part ID"
partsTable.GridColumnStyles.Add(partIdColumn)
DataGrid1.TableStyles.Add(partsTable)
End Sub
Public Class Widget
Private myModel As String
Private myId As String
Private myPrice As Decimal
' Use an ArrayList to create a related collection.
Private myParts As New ArrayList()
Public Property Model() As String
Get
Return myModel
End Get
Set(ByVal Value As String)
myModel = Value
End Set
End Property
Public Property Id() As String
Get
Return myId
End Get
Set(ByVal Value As String)
myId = Value
End Set
End Property
Public Property Parts() As ArrayList
Get
Return myParts
End Get
Set(ByVal Value As ArrayList)
myParts = Value
End Set
End Property
Public Property Price() As Decimal
Get
Return myPrice
End Get
Set(ByVal Value As Decimal)
myPrice = Value
End Set
End Property
End Class
Public Class Part
Private myPartId As String
Public Property PartId() As String
Get
Return myPartId
End Get
Set(ByVal Value As String)
myPartId = Value
End Set
End Property
End Class
Remarques
Pour lier à DataGrid un tableau d’objets fortement typé, le type d’objet doit contenir des propriétés publiques. Pour créer un DataGridTableStyle qui affiche le tableau, définissez la DataGridTableStyle.MappingName propriété sur typename
où typename
est remplacé par le nom du type d’objet. Notez également que la propriété respecte la MappingName casse ; le nom de type doit être mis en correspondance exactement. Consultez la MappingName propriété pour obtenir un exemple.
Vous pouvez également lier à DataGrid un ArrayList. Une fonctionnalité de est ArrayList qu’il peut contenir des objets de plusieurs types, mais ne DataGrid peut se lier à une telle liste que lorsque tous les éléments de la liste sont du même type que le premier élément. Cela signifie que tous les objets doivent être du même type ou hériter de la même classe que le premier élément de la liste. Par exemple, si le premier élément d’une liste est un Control, le deuxième élément peut être un TextBox (qui hérite de Control). Si, en revanche, le premier élément est un TextBox, le deuxième objet ne peut pas être un Control. En outre, le ArrayList doit contenir des éléments lorsqu’il est lié. Un vide ArrayList génère une grille vide. Lors de la liaison à un ArrayList, définissez le MappingName de sur DataGridTableStyle « ArrayList » (le nom du type).
La valeur par défaut est le nom de la liste gérée par pour CurrencyManager cette grille. le CurrencyManager pour est DataGridTableStyle défini à l’aide du DataGridTableStyle constructeur .
L’événement MappingNameChanged se produit lorsque la MappingName valeur change.