views:

100

answers:

1

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:

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.

QueryFilter and QueryFilterNode are just POCO's and contain data of course.

For completeness sake the construction of the 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;
}
A: 

Hmm, it seems it was my own mistake. QueryFilterNode, used as datasource ( BindingList<QueryFilterNode> ) wasn't a POCO but a datacontract. Snippet:

[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.

class QueryFilterNodeSimple
{

  public string FieldCode
  { get; set; }
edosoft