Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
104
Variable number of columns in grid
posted

I have a XamDataGrid that is bound to a list of objects called WorkItem. Each WorkItem can have a varying number of WIProperty objects.

  [DataContract(Name = "WorkItem", Namespace = "mynamespace")]
  public class WorkItem
  {
     [DataMember]
     public string DefinitionName getset; }
 
     [DataMember]
     public string Prioritygetset; }
 
     [DataMember]
     public string Statusgetset; }
     etc.......
    [DataMember]
    public List<WIProperty> Properties { getset; }
}
 
public class WIProperty
{
    [DataMember]
    public string Name { getset; }
  
    [DataMember]
    public object Value; 
}

 I've created the xaml for the table and everything looks great.

<igDP:XamDataGrid.FieldLayouts>
    <igDP:FieldLayout>
        <igDP:FieldLayout.Fields>
            <igDP:Field Label="Definition Name"      Name="DefinitionName"    Visibility="Visible"   Width="InitialAuto" />
            <igDP:Field Label="Priority"             Name="Priority"            Visibility="Visible"   Width="InitialAuto" />
            <igDP:Field Label="Status"               Name="Status"              Visibility="Visible"   Width="InitialAuto" />
            etc, etc...
        </igDP:FieldLayout.Fields>
    </igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>

 

Except for the fact that the list of "WIProperty" initially showed up as a child grid for each record.  I set the "AutoGenerateFields" property in my FieldLayoutSettings to "false", and now the table displays fine for the fact I now need to create columns for the WorkItem properties.

Right now, I am doing a foreach through the list of WorkItems, looking at the WIProperty count, and then dynamically create enough columns to handle the max number of WIProperties that I find.

 

xdgQueue.FieldLayouts[0].Fields.Add(new Field
{
   Name="PropertyName" + (int)(idFieldCount + x),
   DataType=typeof(string),
   Label="Property Name " + (int)(idFieldCount + x)
});
xdgQueue.FieldLayouts[0].Fields.Add(new Field
{
   Name="PropertyValue" + (int)(idFieldCount + x),
   DataType=typeof(string),
   Label="Property Value " + (int)(idFieldCount + x)
});

 

Then, once again I foreach through the list of WIProperties and try to write their values to the grid.

 
 (xdgQueue.Records[curRecIndex] as DataRecord).Cells["PropertyName1"].Value = wi.Properties[x].Name;
 (xdgQueue.Records[curRecIndex] as DataRecord).Cells["PropertyValue1"].Value = wi.Properties[x].Value;

 

However....once I try to set the value, the value remains null. Any ideas or suggestions?
 
Is there a way I can achieve this with bindings so that I can avoid the overhead of doing a foreach through those objects? This seems horribly unnecessary, but I'm not sure how to do it with binding to get everything to show up in the same grid, just as additional columns...




 

  • 138253
    Offline posted

    Hello,

    It has been a while since you have made your post, in case you still need support I will be glad to assist you further. I suppose the other community members can benefit from this answer as well. I have been looking into your post and I suggest you use UnboudFields insetad of Fields and set their BindingPath Property in order to have Binding. You can do something like this:

    UnboundField uf = new UnboundField();
    uf.Label = "MyName";
    uf.BindingPath = new PropertyPath("Properties[i].Name"); 
    xdgQueue.FieldLayouts[0].Fields.Add(uf);
    

     

    Feel free to write me if you have further questions.