DataGridTableStyle.MappingName 属性
获取或设置用于将此表映射到特定数据源的名称。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Property MappingName As String
用法
Dim instance As DataGridTableStyle
Dim value As String
value = instance.MappingName
instance.MappingName = value
public string MappingName { get; set; }
public:
property String^ MappingName {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_MappingName ()
/** @property */
public void set_MappingName (String value)
public function get MappingName () : String
public function set MappingName (value : String)
属性值
用于将此网格映射到特定数据源的名称。
备注
若要将 DataGrid 绑定到对象的强类型数组,该对象类型必须包含公共属性。若要创建一个显示此数组的 DataGridTableStyle,请将 DataGridTableStyle.MappingName 属性设置为 typename,其中 typename 由该对象类型的名称替换。另外请注意,MappingName 属性区分大小写;类型名称必须完全匹配。有关示例,请参见 MappingName 属性。
也可以将 DataGrid 绑定到 ArrayList。ArrayList 的一个功能是它可以包含多种类型的对象,但当列表中的所有项与第一项具有相同的类型时,DataGrid 只能绑定到这类列表。这意味着所有的对象必须是同一种类型,或者必须从与列表中第一项相同的类继承。例如,如果列表中的第一项为 Control,则第二项可能为 TextBox(它从 Control 继承)。另一方面,如果第一项为 TextBox,则第二个对象就不可能是 Control。此外,ArrayList 在绑定时必须包含项目。空 ArrayList 会导致空网格。当绑定到 ArrayList 时,请将 DataGridTableStyle 的 MappingName 设置为“ArrayList”(类型名)。
默认为由该网格的 CurrencyManager 管理的列表名称。DataGridTableStyle 的 CurrencyManager 使用 DataGridTableStyle 构造函数设置。
MappingNameChanged 事件在 MappingName 值更改时发生。
示例
下面的代码示例创建 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
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;}
}
}
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.set_Model("AAA");
tempMachine.set_Id("A100");
tempMachine.set_Price(Convert.ToDecimal(3.8));
machines.set_Item(0, tempMachine);
// The first Machine includes an array of Part objects.
Part p1 = new Part();
p1.set_PartId("PartX");
Part p2 = new Part();
p2.set_PartId("PartY");
// Note that the machines.Parts property returns an ArrayList.
// Add the parts to the ArrayList using the AddRange method.
tempMachine.get_Parts().AddRange(new Part[] { p1, p2 });
tempMachine = new Machine();
tempMachine.set_Model("BBB");
tempMachine.set_Id("B100");
tempMachine.set_Price(Convert.ToDecimal(1.52));
machines.set_Item(1, tempMachine);
tempMachine = new Machine();
tempMachine.set_Id("CCC");
tempMachine.set_Model("B100");
tempMachine.set_Price(Convert.ToDecimal(2.14));
machines.set_Item(2, tempMachine);
dataGrid1.SetDataBinding(machines, "");
CreateTableStyle();
} //BindToArray
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.set_MappingName("Machine[]");
// Sets the AlternatingBackColor so you can see the difference.
MachineTable.set_AlternatingBackColor(System.Drawing.Color.
get_LightBlue());
// Creates three column styles.
DataGridTextBoxColumn modelColumn = new DataGridTextBoxColumn();
modelColumn.set_MappingName("Model");
modelColumn.set_HeaderText("Model");
DataGridTextBoxColumn IdColumn = new DataGridTextBoxColumn();
IdColumn.set_MappingName("Id");
IdColumn.set_HeaderText("Id");
DataGridTextBoxColumn priceColumn = new DataGridTextBoxColumn();
priceColumn.set_MappingName("Price");
priceColumn.set_HeaderText("Price");
priceColumn.set_Format("c");
// Adds the column styles to the grid table style.
MachineTable.get_GridColumnStyles().Add(modelColumn);
MachineTable.get_GridColumnStyles().Add(IdColumn);
MachineTable.get_GridColumnStyles().Add(priceColumn);
// Add the table style to the collection, but clear the
// collection first.
dataGrid1.get_TableStyles().Clear();
dataGrid1.get_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.set_MappingName("ArrayList");
DataGridTextBoxColumn partIdColumn = new DataGridTextBoxColumn();
partIdColumn.set_MappingName("PartID");
partIdColumn.set_HeaderText("Part ID");
partsTable.get_GridColumnStyles().Add(partIdColumn);
dataGrid1.get_TableStyles().Add(partsTable);
} //CreateTableStyle
public class Machine
{
private String model;
private String id;
private System.Decimal price;
// Use an ArrayList to create a related collection.
private ArrayList parts = new ArrayList();
/** @property
*/
public String get_Model()
{
return model;
} //get_Model
/** @property
*/
public void set_Model(String value)
{
model = value;
} //set_Model
/** @property
*/
public String get_Id()
{
return id;
} //get_Id
/** @property
*/
public void set_Id(String value)
{
id = value;
} //set_Id
/** @property
*/
public ArrayList get_Parts()
{
return parts;
} //get_Parts
/** @property
*/
public void set_Parts(ArrayList value)
{
parts = value;
} //set_Parts
/** @property
*/
public System.Decimal get_Price()
{
return price;
} //get_Price
/** @property
*/
public void set_Price(System.Decimal value)
{
price = value;
} //set_Price
} //Machine
public class Part
{
private String partId;
/** @property
*/
public String get_PartId()
{
return partId;
} //get_PartId
/** @property
*/
public void set_PartId(String value)
{
partId = value;
} //set_PartId
} //Part
平台
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
请参见
参考
DataGridTableStyle 类
DataGridTableStyle 成员
System.Windows.Forms 命名空间
CurrencyManager 类
DataGrid 类
DataGridColumnStyle 类
DataGrid.TableStyles 属性