有如下代码:

/**//*
* Created by:
* Created: 2006年12月23日
*/

using System;

namespace TestObjectToDataGrid

...{
public class User

...{
private int id;
private string name;
private float total;
private Address address;

public int Id

...{

get ...{ return id; }

set ...{ id = value; }
}

public string Name

...{

get ...{ return name; }

set ...{ name = value; }
}

public float Total

...{

get ...{ return total; }

set ...{ total = value; }
}

public Address Address

...{

get ...{ return address; }

set ...{ address = value; }
}
public string GetAddress

...{

get ...{ return address.ToString(); }
}
}

public class Address

...{
private string qq;
private string email;

public string Qq

...{

get ...{ return qq; }

set ...{ qq = value; }
}

public string Email

...{

get ...{ return email; }

set ...{ email = value; }
}

public new string ToString()

...{
return email + ":" + qq;
}
}
}
数据源类:

/**//*
* Created by:
* Created: 2006年12月23日
*/

using System.Collections;

namespace TestObjectToDataGrid

...{
public class DataSource

...{
public static IList GetDs()

...{
IList list = new ArrayList();
for (int i = 0; i < 10; i++)

...{
User user = new User();
user.Id = i;
user.Name = "name" + i;
user.Total = i*9.9f;
Address add = new Address();
add.Qq = "qq" + i;
add.Email = "email" + i;
user.Address = add;
list.Add(user);
}
return list;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestObjectToDataGrid

...{

/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form

...{
private System.Windows.Forms.DataGrid dgUsers;
private System.Windows.Forms.Button btnLoad;
private System.Windows.Forms.DataGridTableStyle dgsMain;
private System.Windows.Forms.DataGridTextBoxColumn dgcName;
private System.Windows.Forms.DataGridTextBoxColumn dgcId;
private System.Windows.Forms.DataGridTextBoxColumn dgcQq;
private System.Windows.Forms.DataGridTextBoxColumn dgcEmail;
private System.Windows.Forms.ComboBox ddlUser;

/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()

...{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}


/**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )

...{
if( disposing )

...{
if (components != null)

...{
components.Dispose();
}
}
base.Dispose( disposing );
}


Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码

/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()

...{
this.dgUsers = new System.Windows.Forms.DataGrid();
this.dgsMain = new System.Windows.Forms.DataGridTableStyle();
this.dgcName = new System.Windows.Forms.DataGridTextBoxColumn();
this.dgcId = new System.Windows.Forms.DataGridTextBoxColumn();
this.dgcQq = new System.Windows.Forms.DataGridTextBoxColumn();
this.dgcEmail = new System.Windows.Forms.DataGridTextBoxColumn();
this.btnLoad = new System.Windows.Forms.Button();
this.ddlUser = new System.Windows.Forms.ComboBox();
((System.ComponentModel.ISupportInitialize)(this.dgUsers)).BeginInit();
this.SuspendLayout();
//
// dgUsers
//
this.dgUsers.DataMember = "";
this.dgUsers.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgUsers.Location = new System.Drawing.Point(0, 64);
this.dgUsers.Name = "dgUsers";
this.dgUsers.Size = new System.Drawing.Size(292, 208);
this.dgUsers.TabIndex = 0;

this.dgUsers.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] ...{
this.dgsMain});
//
// dgsMain
//
this.dgsMain.DataGrid = this.dgUsers;

this.dgsMain.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] ...{
this.dgcName,
this.dgcId,
this.dgcQq,
this.dgcEmail});
this.dgsMain.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgsMain.MappingName = "";
//
// dgcName
//
this.dgcName.Format = "";
this.dgcName.FormatInfo = null;
this.dgcName.HeaderText = "姓名";
this.dgcName.MappingName = "Name";
this.dgcName.Width = 75;
//
// dgcId
//
this.dgcId.Format = "";
this.dgcId.FormatInfo = null;
this.dgcId.HeaderText = "学号";
this.dgcId.MappingName = "Id";
this.dgcId.Width = 75;
//
// dgcQq
//
this.dgcQq.Format = "";
this.dgcQq.FormatInfo = null;
this.dgcQq.HeaderText = "总额";
this.dgcQq.MappingName = "total";
this.dgcQq.Width = 75;
//
// dgcEmail
//
this.dgcEmail.Format = "";
this.dgcEmail.FormatInfo = null;
this.dgcEmail.HeaderText = "电子邮件";
this.dgcEmail.MappingName = "GetAddress";
this.dgcEmail.Width = 150;
//
// btnLoad
//
this.btnLoad.Location = new System.Drawing.Point(24, 20);
this.btnLoad.Name = "btnLoad";
this.btnLoad.Size = new System.Drawing.Size(68, 24);
this.btnLoad.TabIndex = 1;
this.btnLoad.Text = "Load";
this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
//
// ddlUser
//
this.ddlUser.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.ddlUser.Location = new System.Drawing.Point(116, 20);
this.ddlUser.Name = "ddlUser";
this.ddlUser.Size = new System.Drawing.Size(116, 20);
this.ddlUser.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.ddlUser);
this.Controls.Add(this.btnLoad);
this.Controls.Add(this.dgUsers);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dgUsers)).EndInit();
this.ResumeLayout(false);

}
#endregion


/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()

...{
Application.Run(new Form1());
}

private void btnLoad_Click(object sender, System.EventArgs e)

...{
IList list = DataSource.GetDs();
dgUsers.DataSource = list;
dgsMain.DataGrid = dgUsers;
dgsMain.MappingName = "ArrayList";
ddlUser.DataSource = list;
ddlUser.DisplayMember = "Name";
ddlUser.ValueMember = "Id";
}

private void btnShow_Click(object sender, System.EventArgs e)

...{
}
}
}
在这里我没有找到直接将Address类的Qq属性绑定到DataGrid的方法,我只能通过多做一个只读属性,才能在DataGrid中显示。哪位高人有没有好办法,分享一下。


































































































































将Address中的属性绑定到DataGrid中,有如下代码:












































































































































































































