在winform中:
需求:我现在用DataGridView显示一组数据,其中有一列想显示为combox,combox的数据源用枚举提供。
问题:在显示的时候,combox这一列老是显示不出来。不知道为什么。
下面是combox的数据源:
private DataTable GetComBoxDataSource()
{
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn( "Text", typeof (string)));
dt.Columns.Add(new DataColumn("Values", typeof(string))); string[] enumns = Enum.GetNames(typeof (ZCTTDataType)); for (int i = 0; i < enumns.Length;i++ )
{
DataRow row = dt.NewRow(); switch (enumns[i])
{
case "dateTime":
row["Text"] = "dateTime";
row["Values"] = "dateTime";
break;
case "unsignedShort":
row["Text"] = "unsignedShort";
row["Values"] = "Values";
break;
case "ZCTTint":
row["Text"] = "int";
row["Values"] = "ZCTTint";
break;
case "unsignedByte":
row["Text"] = "unsignedByte";
row["Values"] = "unsignedByte";
break;
case "ZCTTstring":
row["Text"] = "string";
row["Values"] = "ZCTTstring";
break;
case "ZCTTlong":
row["Text"] = "long";
row["Values"] = "ZCTTlong";
break;
case "binary":
row["Text"] = "binary";
row["Values"] = "binary";
break;
default:
break;
}
dt.Rows.Add(row);
}
return dt;
}在加载的时候,datagirdview的显示是下面的代码:
//显示数据 SetSettingGridViewDisplay(); bindingSource.DataSource = itemAttributeDataTable;
this.GV_ColumnInfo.DataSource = bindingSource; DataGridViewColumn fieldNameColumn = new DataGridViewTextBoxColumn();
fieldNameColumn.HeaderText = "field_name";
fieldNameColumn.DataPropertyName = "field-name";
this.GV_ColumnInfo.Columns.Add(fieldNameColumn); DataGridViewColumn idColumn = new DataGridViewTextBoxColumn();
idColumn.HeaderText = "id";
idColumn.DataPropertyName = "id";
this.GV_ColumnInfo.Columns.Add(idColumn); DataGridViewColumn identifierColumn = new DataGridViewTextBoxColumn();
identifierColumn.HeaderText = "identifier";
identifierColumn.DataPropertyName = "identifier";
this.GV_ColumnInfo.Columns.Add(identifierColumn); DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();
typeColumn.DataSource = GetComBoxDataSource();
typeColumn.Width = 100;
typeColumn.DataPropertyName = "type";
typeColumn.DisplayMember = "Text";
typeColumn.ValueMember = "Values";
typeColumn.HeaderText = "type";
需求:我现在用DataGridView显示一组数据,其中有一列想显示为combox,combox的数据源用枚举提供。
问题:在显示的时候,combox这一列老是显示不出来。不知道为什么。
下面是combox的数据源:
private DataTable GetComBoxDataSource()
{
DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn( "Text", typeof (string)));
dt.Columns.Add(new DataColumn("Values", typeof(string))); string[] enumns = Enum.GetNames(typeof (ZCTTDataType)); for (int i = 0; i < enumns.Length;i++ )
{
DataRow row = dt.NewRow(); switch (enumns[i])
{
case "dateTime":
row["Text"] = "dateTime";
row["Values"] = "dateTime";
break;
case "unsignedShort":
row["Text"] = "unsignedShort";
row["Values"] = "Values";
break;
case "ZCTTint":
row["Text"] = "int";
row["Values"] = "ZCTTint";
break;
case "unsignedByte":
row["Text"] = "unsignedByte";
row["Values"] = "unsignedByte";
break;
case "ZCTTstring":
row["Text"] = "string";
row["Values"] = "ZCTTstring";
break;
case "ZCTTlong":
row["Text"] = "long";
row["Values"] = "ZCTTlong";
break;
case "binary":
row["Text"] = "binary";
row["Values"] = "binary";
break;
default:
break;
}
dt.Rows.Add(row);
}
return dt;
}在加载的时候,datagirdview的显示是下面的代码:
//显示数据 SetSettingGridViewDisplay(); bindingSource.DataSource = itemAttributeDataTable;
this.GV_ColumnInfo.DataSource = bindingSource; DataGridViewColumn fieldNameColumn = new DataGridViewTextBoxColumn();
fieldNameColumn.HeaderText = "field_name";
fieldNameColumn.DataPropertyName = "field-name";
this.GV_ColumnInfo.Columns.Add(fieldNameColumn); DataGridViewColumn idColumn = new DataGridViewTextBoxColumn();
idColumn.HeaderText = "id";
idColumn.DataPropertyName = "id";
this.GV_ColumnInfo.Columns.Add(idColumn); DataGridViewColumn identifierColumn = new DataGridViewTextBoxColumn();
identifierColumn.HeaderText = "identifier";
identifierColumn.DataPropertyName = "identifier";
this.GV_ColumnInfo.Columns.Add(identifierColumn); DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();
typeColumn.DataSource = GetComBoxDataSource();
typeColumn.Width = 100;
typeColumn.DataPropertyName = "type";
typeColumn.DisplayMember = "Text";
typeColumn.ValueMember = "Values";
typeColumn.HeaderText = "type";
解决方案 »
- C#自动化测试问题
- 打开Excel问题 + 5裤散分
- 能不能重载=操作符?
- 用数组绑定listbox,如果默认全部选中啊?
- 在线等:asp.net如何在客户端输出XML?
- 使用Server.MapPath后,网页中引用相对路径的图片打不开
- 好郁闷好郁闷好郁闷~ 查了.NET Framework文档,明明有System.Web.UI命令时间,放到程序里编译说找不到???
- Windows桌面应用程序中如何做动态直方图
- 如何得到datagrid中某一单元格控件的句柄
- 怎么根据数据库中的表自动的生成界面.以后修改数据库的结构,不用改程序,只要改别的东西就可以了??情大家给个思路.我以前没有做过!!谢谢
- 如何在datagridview中指定的单元格输入数字
- C# 调用NAudio.dll只有一台电脑上麦克风可用
comboBox1.DataSource = dat.Tables[0].DefaultView;
comboBox1.DisplayMember = "测试列";
comboBox1.ValueMember = "ColumnName";