DataGridViewTextBoxColumn 绑定问题,郁闷啊... 本帖最后由 shadan05 于 2010-12-01 10:19:59 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.njxsw.com/forum-70-1.html去这里问一下 思路:把XML的内容读出来 写到一个DataTable里去DataGridViewk控件 可以灵活的定义列名的d_dataGridView绑定这个DataTable就行 下拉列表绑定时候,DataPropertyName和ValueMember做对比, //下拉列表绑定时候,DataPropertyName和ValueMember做对比, DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn(); typeColumn.DataSource = GetComBoxDataSource(); typeColumn.Width = 200; typeColumn.DataPropertyName = "type"; typeColumn.DisplayMember = "Text"; typeColumn.ValueMember = "Values"; typeColumn.HeaderText = "type"; //typeColumn = ComboBoxStyle.DropDownList; this.GV_ColumnInfo.Columns.Add(typeColumn); 下面是combox的数据源,一般为name和key 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"] = "unsignedShort"; break; case "ZCTTint": row["Text"] = "int"; row["Values"] = "int"; break; case "unsignedByte": row["Text"] = "unsignedByte"; row["Values"] = "unsignedByte"; break; case "ZCTTstring": row["Text"] = "string"; row["Values"] = "string"; break; case "ZCTTlong": row["Text"] = "long"; row["Values"] = "long"; break; case "binary": row["Text"] = "binary"; row["Values"] = "binary"; break; default: break; } dt.Rows.Add(row); } return dt; } 就是说 typeColumn.DataSource = 每行都是一个不同的数据。问题就在这儿,我该咋样为每行绑定不同的数据源呢? DataGridViewComboBoxColumn obj = new DataGridViewComboBoxColumn(); obj.HeaderText = headerText; dgv.Columns.Insert(1, obj); for (int i = 0; i < dgv.Rows.Count - 1; i++) { ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DataSource = GetAuthors(dgv.Rows[i].Cells[0].Value.ToString()); ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value"; ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key"; }我循环在把每行的数据源取到,然后绑定,为啥是空呢?实际上是有值的. 首先你的标题与内容不符哦.希望以后注意一下吧!方便大家能更好地回答问题.看见你代码里面有:obj.DataSource = ?;你的数据源应该是有的了恰好不久我也在琢磨这个问题.你可以这样试试:右键DataGridView->编辑列->增加DataGridViewComboBoxColumn(注意:属性列里面的displaymember和valuemember按自己的需要设定一下,DataPropertyName与数据源的名称一致.)作好以上准备后就可以在加载画面的时候调用((DataGridViewComboBoxColumn)dataGridView1.Columns["DataPropertyName"]).DataSource = ?进行绑定了.顺便提一下:下拉列的数据绑定跟整个DataGridView数据源的绑定 都必须要分别做的.希望能帮你解决这个问题. ((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DataSource = GetAuthors(dgv.Rows[i].Cells[0].Value.ToString());((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value";((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key";不知道为什么是空值。 函数GetAuthors是做什么的?它的返回值的结构是一个table? List<LWKeyValueArg> 是这个对象集合,每个对象有key、value俩个属性。 我是读到对象集合里,和dataTable不一样的吗? d_dataGridView.AutoGenerateColumns = false; //添加名字 DataGridViewTextBoxColumn objText = new DataGridViewTextBoxColumn(); objText.DataPropertyName = "Item"; objText.HeaderText = "名称"; objText.Resizable = DataGridViewTriState.True; d_dataGridView.Columns.Add(objText); //添加作者 DataGridViewComboBoxColumn objComboBox = new DataGridViewComboBoxColumn(); objComboBox.DataSource = list; objComboBox.DataPropertyName = "Author"; objComboBox.HeaderText = "作者"; objComboBox.ValueMember = "Author"; objComboBox.DisplayMember = "Author"; d_dataGridView.Columns.Add(objComboBox); d_dataGridView.DataSource = list; http://topic.csdn.net/u/20091113/17/e7152fb7-a1d1-430d-9b43-d6c50c5ab757.html 请问:如何给treeview的子节点添加click事件呢? 求好心人帮帮忙,我是一个新人,给位大哥大姐帮帮忙啊,急急急 水晶报表速度问题?? richtextbox自动滚动问题 求救:C# 和 mysql 共同使用时的乱码问题 有关dataset 请各位给这个小软件估个价。。来着有分。。先来先得。。 (.net)问大家一个xml更新信息问题. 请教关于两个文本内容比对的算法! 我单独开一个线程,出现cpu基本占用完全,里面几条语句!出现莫名错误! 请高手指教,(问题最简单) 影藏显示 讨论一下C#的效率问题
去这里问一下
把XML的内容读出来 写到一个DataTable里去
DataGridViewk控件 可以灵活的定义列名的d_dataGridView绑定这个DataTable就行
DataGridViewComboBoxColumn typeColumn = new DataGridViewComboBoxColumn();
typeColumn.DataSource = GetComBoxDataSource();
typeColumn.Width = 200;
typeColumn.DataPropertyName = "type";
typeColumn.DisplayMember = "Text";
typeColumn.ValueMember = "Values";
typeColumn.HeaderText = "type";
//typeColumn = ComboBoxStyle.DropDownList;
this.GV_ColumnInfo.Columns.Add(typeColumn);
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"] = "unsignedShort";
break;
case "ZCTTint":
row["Text"] = "int";
row["Values"] = "int";
break;
case "unsignedByte":
row["Text"] = "unsignedByte";
row["Values"] = "unsignedByte";
break;
case "ZCTTstring":
row["Text"] = "string";
row["Values"] = "string";
break;
case "ZCTTlong":
row["Text"] = "long";
row["Values"] = "long";
break;
case "binary":
row["Text"] = "binary";
row["Values"] = "binary";
break;
default:
break;
}
dt.Rows.Add(row);
}
return dt;
}
就是说 typeColumn.DataSource = 每行都是一个不同的数据。
问题就在这儿,我该咋样为每行绑定不同的数据源呢?
DataGridViewComboBoxColumn obj = new DataGridViewComboBoxColumn();
obj.HeaderText = headerText;
dgv.Columns.Insert(1, obj); for (int i = 0; i < dgv.Rows.Count - 1; i++)
{
((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DataSource = GetAuthors(dgv.Rows[i].Cells[0].Value.ToString());
((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value";
((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key";
}
我循环在把每行的数据源取到,然后绑定,为啥是空呢?实际上是有值的.
你可以这样试试:
右键DataGridView->编辑列->增加DataGridViewComboBoxColumn(注意:属性列里面的displaymember和valuemember按自己的需要设定一下,DataPropertyName与数据源的名称一致.)
作好以上准备后就可以在加载画面的时候调用
((DataGridViewComboBoxColumn)dataGridView1.Columns["DataPropertyName"]).DataSource = ?进行绑定了.
顺便提一下:下拉列的数据绑定跟整个DataGridView数据源的绑定 都必须要分别做的.
希望能帮你解决这个问题.
((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).DisplayMember = "Value";
((DataGridViewComboBoxCell)dgv.Rows[i].Cells[1]).ValueMember = "Key";
不知道为什么是空值。
我是读到对象集合里,和dataTable不一样的吗?
d_dataGridView.AutoGenerateColumns = false;
//添加名字
DataGridViewTextBoxColumn objText = new DataGridViewTextBoxColumn();
objText.DataPropertyName = "Item";
objText.HeaderText = "名称";
objText.Resizable = DataGridViewTriState.True;
d_dataGridView.Columns.Add(objText); //添加作者
DataGridViewComboBoxColumn objComboBox = new DataGridViewComboBoxColumn();
objComboBox.DataSource = list;
objComboBox.DataPropertyName = "Author";
objComboBox.HeaderText = "作者";
objComboBox.ValueMember = "Author";
objComboBox.DisplayMember = "Author";
d_dataGridView.Columns.Add(objComboBox); d_dataGridView.DataSource = list;