DataGridViewComboBoxColumn这个东西初次看见的时候以为是个特别方便的方式在DataGridView中显示下拉框,结果盲目的试了半天也无法让程序运行起来.
网上的例子试了好多个,也不明白.结果才在msdn的一个相关话题中找到个例子,算是明白了.
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
贴上自己从datatable绑定成功的第一个例子.
数据库的表很简单:一个简单的左链接,将状态的字段链接进来.
CREATE TABLE [dbo].[order_info](
[order_id] [bigint] NOT NULL,
[user_id] [bigint] NOT NULL,
[item_name] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[item_price] [money] NULL,
[order_state_id] [smallint] NULL,
CREATE TABLE [dbo].[order_state](
[order_state_id] [smallint] NULL,
[order_state_name] [nchar](10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
private void initData()
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AutoGenerateColumns = false;
string sql1 = @"SELECT order_state.order_state_id, order_info.order_id
FROM order_info LEFT OUTER JOIN
order_state ON order_info.order_state_id = order_state.order_state_id";
string sql2 = @"SELECT order_state_name, order_state_id
FROM order_state";
DataTable dt = GetTable(sql1);
dataGridView1.DataSource = dt;//一定要在dgvComboBoxColumn的DataSource后设置
DataGridViewComboBoxColumn dgvComboBoxColumn = new DataGridViewComboBoxColumn();//dataGridView1.Columns[0] as DataGridViewComboBoxColumn;
dgvComboBoxColumn.DataPropertyName = "order_state_id";
dgvComboBoxColumn.DataSource = GetTable(sql2).DefaultView;//必须在设置dataGridView1的DataSource的属性前设置
dgvComboBoxColumn.DisplayMember = "order_state_name";
dgvComboBoxColumn.ValueMember = "order_state_id";
dataGridView1.Columns.Add(dgvComboBoxColumn);
DataGridViewColumn col = new DataGridViewTextBoxColumn();
col.DataPropertyName = "order_id";
col.Name = "order_id";
dataGridView1.Columns.Add(col);
Console.WriteLine("colcount={0} name={1}", dataGridView1.ColumnCount,dataGridView1.Columns[0].Name);
}
private DataTable GetTable(string sql)
{
String constr = "Data Source=ZGC-20110302GHR//SQLEXPRESS;Initial Catalog=DbTest;User ID=sa;Password=java";
using (SqlConnection sqlconn = new SqlConnection(constr))
{
DataTable dt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter(sql, sqlconn);
sqlda.Fill(dt);
return dt;
}
}