本文整理汇总了C#中System.Windows.Forms.DataGridTableStyle.MappingName属性的典型用法代码示例。如果您正苦于以下问题:C# DataGridTableStyle.MappingName属性的具体用法?C# DataGridTableStyle.MappingName怎么用?C# DataGridTableStyle.MappingName使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Windows.Forms.DataGridTableStyle
的用法示例。
在下文中一共展示了DataGridTableStyle.MappingName属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindToArray
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;}
}
}
示例2: DataGridColumnStyle
//引入命名空间
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class DataGridColumnStyle : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid employeeDataGrid;
private System.ComponentModel.Container components = null;
public DataGridColumnStyle()
{
InitializeComponent();
string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;";
string commandString = "Select ID, FirstName from Employee ";
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet,"Employee");
DataTable dataTable = dataSet.Tables[0];
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = dataTable.TableName;
GridColumnStylesCollection columnStyles = tableStyle.GridColumnStyles;
DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName="ID";
columnStyle.HeaderText = "Employee ID";
columnStyles.Add(columnStyle);
columnStyle = new DataGridTextBoxColumn();
columnStyle.MappingName = "FirstName";
columnStyle.HeaderText="Employee First Name";
columnStyles.Add(columnStyle);
GridTableStylesCollection tableStyles = employeeDataGrid.TableStyles;
tableStyles.Add(tableStyle);
employeeDataGrid.DataSource=dataTable;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.employeeDataGrid = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.employeeDataGrid)).BeginInit();
this.SuspendLayout();
//
// employeeDataGrid
//
this.employeeDataGrid.DataMember = "";
this.employeeDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.employeeDataGrid.Location = new System.Drawing.Point(8, 16);
this.employeeDataGrid.Name = "employeeDataGrid";
this.employeeDataGrid.Size = new System.Drawing.Size(448, 176);
this.employeeDataGrid.TabIndex = 0;
//
// DataGridColumnStyle
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 205);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.employeeDataGrid});
this.Name = "DataGridColumnStyle";
this.Text = "DataGridColumnStyle";
((System.ComponentModel.ISupportInitialize)(this.employeeDataGrid)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new DataGridColumnStyle());
}
}