DataGridTableStyle.MappingName プロパティ
テーブルを特定のデータ ソースに割り当てるときに使用する名前を取得または設定します。
Public Property MappingName As String
[C#]
public string MappingName {get; set;}
[C++]
public: __property String* get_MappingName();public: __property void set_MappingName(String*);
[JScript]
public function get MappingName() : String;public function set MappingName(String);
プロパティ値
グリッドを特定のデータ ソースに割り当てるときに使用する名前。
解説
System.Windows.Forms.DataGrid をオブジェクトの厳密に型指定された配列にバインドするには、オブジェクトにパブリック プロパティが含まれている必要があります。このような配列を表示する DataGridTableStyle を作成するには、 MappingName プロパティを classname[] に設定します。なお、この classname は実際のクラス名で置き換えてください。また、 MappingName プロパティでは大文字と小文字が区別されます。
また、 DataGrid を ArrayList にバインドできます。 ArrayList の特長は、複数の型のオブジェクトを格納できることです。ただし、 DataGrid はリスト内のすべての項目の型が 1 番目の項目の型と同じ場合に限り、リストにバインドできます。つまり、すべてのオブジェクトの型が同じであるか、リスト内の最初の項目と同じクラスからすべてのクラスが継承している必要があります。たとえば、リスト内の最初の項目が Control オブジェクトの場合、2 番目の項目は (Control から継承した) TextBox にできます。逆に、1 番目の項目が TextBox の場合、2 番目のオブジェクトが Control になることはできません。さらに、 ArrayList は、バインディングするときは、項目を含んでいる必要があります。空の ArrayList の場合、空のグリッドとなります。 ArrayList にバインディングするときは、 DataGridTableStyle の MappingName を "ArrayList" (型の名前) に設定します。
既定値は、グリッドの CurrencyManager で管理されるリストの名前です。 DataGridTableStyle の CurrencyManager は、 DataGridTableStyle コンストラクタを使用して設定されます。
MappingNameChanged イベントは、 MappingName の値が変更されると発生します。
使用例
[Visual Basic, C#, C++] Widget
オブジェクトの配列を作成し、 System.Windows.Forms.DataGrid コントロールをこの配列にバインドする例を次に示します。この例では、次に、 DataGridTableStyle を作成し、クラス名に角かっこを付けた文字列を MappingName に設定しています。
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
[C#]
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;}
}
}
[C++]
private:
void BindToArray() {
// Create an array of Machine objects (defined below).
Machine* Machines[] = new Machine*[3];
Machine* tempMachine;
tempMachine = new Machine();
tempMachine->Model = S"AAA";
tempMachine->Id= S"A100";
tempMachine->Price= Convert::ToDecimal(3.80);
Machines->Item[0]=tempMachine;
// The first Machine includes an array of Part objects.
Part* p1 = new Part();
p1->PartId= S"PartX";
Part* p2 = new Part();
p2->PartId= S"PartY";
// Note that the Machines::Parts property returns an ArrayList.
// Add the parts to the ArrayList using the AddRange method.
Part* temp0[] = {p1, p2};
tempMachine->Parts->AddRange(
dynamic_cast<System::Collections::ICollection*>(temp0));;
tempMachine = new Machine();
tempMachine->Model = S"BBB";
tempMachine->Id= S"B100";
tempMachine->Price= Convert::ToDecimal(1.52);
Machines->Item[1]=tempMachine;
tempMachine = new Machine();
tempMachine->Id= S"CCC";
tempMachine->Model = S"B100";
tempMachine->Price= Convert::ToDecimal(2.14);
Machines->Item[2] = tempMachine;
dataGrid1->SetDataBinding(Machines, S"");
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= S"Machine->Item[]";
// 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= S"Model";
modelColumn->HeaderText= S"Model";
DataGridTextBoxColumn* IdColumn = new DataGridTextBoxColumn();
IdColumn->MappingName= S"Id";
IdColumn->HeaderText= S"Id";
DataGridTextBoxColumn* priceColumn = new DataGridTextBoxColumn();
priceColumn->MappingName= S"Price";
priceColumn->HeaderText= S"Price";
priceColumn->Format = S"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= S"ArrayList";
DataGridTextBoxColumn* partIdColumn = new DataGridTextBoxColumn();
partIdColumn->MappingName= S"PartID";
partIdColumn->HeaderText = S"Part ID";
partsTable->GridColumnStyles->Add(partIdColumn);
dataGrid1->TableStyles->Add(partsTable);
};
__gc class Machine {
private:
String* model;
String* id;
Decimal price;
// Use an ArrayList to create a related collection.
ArrayList* parts;
public:
Machine() {
parts = new ArrayList();
}
__property String* get_Model() {
return model;
}
__property void set_Model(String* value) {
model=value;
}
__property String* get_Id() {
return id;
}
__property void set_Id(String* value) {
id = value;
}
__property ArrayList* get_Parts() {
return parts;
}
__property void set_Parts(ArrayList* value) {
parts = value;
}
__property Decimal get_Price() {
return price;
}
__property void set_Price(Decimal value) {
price = value;
}
};
public:
__gc class Part {
private:
String* partId;
public:
__property String* get_PartId() {
return partId;
}
__property void set_PartId(String* value) {
partId = value;
}
};
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
参照
DataGridTableStyle クラス | DataGridTableStyle メンバ | System.Windows.Forms 名前空間 | CurrencyManager | DataGrid | DataGridColumnStyle | TableStyles