I'm developing a Windows Forms application in VS2008. I want to display a unknown, but small number of DataGridViews on a form, using code like this:
我正在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);
}
The grid(s) are added to the panel, but the data inside is not displayed. My guess is setting the DataSource property doesn't actualy bind the grid, because (for example) the dataGridView_ColumnAdded event is not fired.
网格添加到面板,但不显示内部数据。我的猜测是设置DataSource属性不会实际绑定网格,因为(例如)dataGridView_ColumnAdded事件未被触发。
QueryFilter and QueryFilterNode are just POCO's and contain data of course.
QueryFilter和QueryFilterNode只是POCO,当然包含数据。
For completeness sake the construction of the DataGridView:
为了完整起见,构建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;
}
1 个解决方案
#1
Hmm, it seems it was my own mistake. QueryFilterNode, used as datasource ( BindingList<QueryFilterNode>
) wasn't a POCO but a datacontract. Snippet:
嗯,这似乎是我自己的错误。用作数据源的QueryFilterNode(BindingList
[DataContract(Name = "QueryFilterNode")]
public class QueryFilterNode
{
[DataMember(IsRequired = true)]
public string FieldCode;
For some reason these cannot be databound. I used a simple class like this in my BindingList and it just worked.
由于某些原因,这些不能数据绑定。我在BindingList中使用了这样一个简单的类,它只是起作用。
class QueryFilterNodeSimple
{
public string FieldCode
{ get; set; }
#1
Hmm, it seems it was my own mistake. QueryFilterNode, used as datasource ( BindingList<QueryFilterNode>
) wasn't a POCO but a datacontract. Snippet:
嗯,这似乎是我自己的错误。用作数据源的QueryFilterNode(BindingList
[DataContract(Name = "QueryFilterNode")]
public class QueryFilterNode
{
[DataMember(IsRequired = true)]
public string FieldCode;
For some reason these cannot be databound. I used a simple class like this in my BindingList and it just worked.
由于某些原因,这些不能数据绑定。我在BindingList中使用了这样一个简单的类,它只是起作用。
class QueryFilterNodeSimple
{
public string FieldCode
{ get; set; }