您的位置:首页 > 编程语言 > C#

c#——Winform DatagridView不同行不同控件显示

2016-09-08 13:31 246 查看
DataGridViewRow row = new DataGridViewRow();

DataGridViewTextBoxCell textboxcell = new DataGridViewTextBoxCell();

textboxcell.Value = "aaa";

row.Cells.Add(textboxcell);

DataGridViewComboBoxCell comboxcell = new DataGridViewComboBoxCell();

row.Cells.Add(comboxcell);

dataGridView1.Rows.Add(row);

DataGridViewRow row = new DataGridViewRow();是创建DataGridView的行对象,DataGridViewTextBoxCell是单元格的内容是个TextBox,DataGridViewComboBoxCell是单元格的内容是下拉列表框,同理可知,DataGridViewButtonCell是单元格的内容是个按钮,等等。textboxcell是新创建的单元格的对象,可以为该对象添加其属性。然后通过row.Cells.Add(textboxcell)为row对象添加textboxcell单元格。要添加其他的单元格,用同样的方法即可。

最后通过dataGridView1.Rows.Add(row)为dataGridView1控件添加新的行row。

如此绑定:给你要显示那列DataGridViewComboBoxColumn 取名为combo吧

则代码如下combo = new DataGridViewComboBoxColumn();

         combo.DataSource = Enum.GetValues(typeof(enumType));

         combo.DataPropertyName = "enumField";

         combo.Name = "enumField";

enumType为你要绑定的枚举类型

enumField为你要选的数据源字段
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: