http://blog.csdn.net/yuri99/article/details/6256755
msdn链接:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.datagridviewcolumn.datapropertyname(v=vs.80).aspx#Y129
摘录 一段明显的代码:
private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
{
// Populate the data source.
bindingSource1.Add(new Knight(Title.King, "Uther", true));
bindingSource1.Add(new Knight(Title.King, "Arthur", true));
bindingSource1.Add(new Knight(Title.Sir, "Mordred", false));
bindingSource1.Add(new Knight(Title.Sir, "Gawain", true));
bindingSource1.Add(new Knight(Title.Sir, "Galahad", true));
// Initialize the DataGridView.
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = true;
dataGridView1.DataSource = bindingSource1;
dataGridView1.Columns.Add(CreateComboBoxWithEnums());
// Initialize and add a text box column.
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = "Name";
column.Name = "Knight";
dataGridView1.Columns.Add(column);
// Initialize and add a check box column.
column = new DataGridViewCheckBoxColumn();
column.DataPropertyName = "GoodGuy";
column.Name = "Good";
dataGridView1.Columns.Add(column);
// Initialize the form.
this.Controls.Add(dataGridView1);
this.AutoSize = true;
this.Text = "DataGridView object binding demo";
}
DataGridViewComboBoxColumn CreateComboBoxWithEnums()
{
DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
combo.DataSource = Enum.GetValues(typeof(Title));
combo.DataPropertyName = "Title";
combo.Name = "Title";
return combo;
}
private class Knight
{
private string hisName;
private bool good;
private Title hisTitle;
public Knight(Title title, string name, bool good)
{
hisTitle = title;
hisName = name;
this.good = good;
}
.....这种类就很明显理解了.
重点注意的一个地方就是设置dataGridView为非自动添加列的,否则绑定数据源后,会自动生成列.
这里的做法就是绑定数据源,但是让它不自动生成,代替用手工添加dataGridView中的列,并设置列需要映射到数据源的哪个字段上.
2.网上看到个非数据绑定的做法,非常的自定义,用起来应该很方便,就是代码量上可能多了些.
地址:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.html