我正在VS2008中开发Windows窗体应用程序。我想在表单上显示一个未知但数量很少的DataGridViews,使用如下代码:
foreach (QueryFilter f in Query.Filter)
{
DataGridView grid = CreateGridView(String.Format("GridView{0}", filters.Count));
grid.Location = new System.Drawing.Point(3, 9 + (filters.Count * grid.Height + 9));
BindingList<QueryFilterNode> nodes = new BindingList<QueryFilterNode>();
foreach (QueryFilterNode node in f)
nodes.Add(node);
grid.DataSource = nodes;
panel1.Controls.Add(grid);
filters.Add(nodes);
}
网格添加到面板中,但不显示内部数据。我的猜测是设置DataSource属性并不实际绑定网格,因为(例如)dataGridView_ColumnAdded事件未被触发。
QueryFilter和QueryFilterNode只是POCO,当然包含数据。
为了完整起见,构建DataGridView:
private DataGridView CreateGridView(string name)
{
DataGridView grid = new DataGridView();
grid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
grid.Name = name;
grid.Size = new System.Drawing.Size(484, 120);
grid.ColumnAdded += new System.Windows.Forms.DataGridViewColumnEventHandler(this.dataGridView_ColumnAdded);
return grid;
}
答案 0 :(得分:0)
BindingList<QueryFilterNode>
)的QueryFilterNode不是POCO而是数据合同。片段:
[DataContract(Name = "QueryFilterNode")]
public class QueryFilterNode
{
[DataMember(IsRequired = true)]
public string FieldCode;
由于某些原因,这些不能数据绑定。我在BindingList中使用了这样一个简单的类,它只是起作用。
class QueryFilterNodeSimple
{
public string FieldCode
{ get; set; }