0

I have an list of objects which each have a list of attributes. In the grid displaying the list of objects, I to show as the attributes as columns instead of showing them in a subgrid.

public class MyObject {
   public int Id {get; set;}
   public string Name {get; set;}
   public IEnumerable<MyAttribute> Attributes {get; set;}
}
public class MyAttribute {
   public int Id {get; set;}
   public string Key {get; set;}
   public string Value {get; set;}
}

So what I have is a IEnumerable<MyObject>. e.g.

  var lst = new[]
  {
     new MyObject
        {
           Id = 1,
           Name = "o1",
           Attributes = new[]
                          {
                             new MyAttribute
                                {
                                   Id = 1,
                                   Key = "k",
                                   Value = "v1"
                                },
                             new MyAttribute
                                {
                                   Id = 2,
                                   Key = "x",
                                   Value = "v2"
                                },
                          }
        },
     new MyObject
        {
           Id = 2,
           Name = "o2",
           Attributes = new[]
                          {
                             new MyAttribute
                                {
                                   Id = 3,
                                   Key = "k",
                                   Value = "v11"
                                },
                             new MyAttribute
                                {
                                   Id = 4,
                                   Key = "x",
                                   Value = "v22"
                                },
                          }
        }
  };

And the grid I would like to end up with would then have 4 columns, Id, Name, k, x.

I have 2 problems...

  1. The component I use to display the grid, uses ModelMetadataProviders.Current.GetMetadataForType((Func<object>) null, typeof (TModel)) to figure out which columns to show. How do I convince the ModelMetadataProviders to include these extra columns?

  2. Similar, the code to get the values ModelMetadataProviders.Current.GetMetadataForProperties(this.Value, typeof (TModel)), how do I convince it to send those extra values?

If I can find a solution for the 2nd problem, I can probably find a workaround for the first one, since that is just a helper class.

Cine
  • 4,255
  • 26
  • 46
  • 1
    Probably [the answer](http://stackoverflow.com/questions/5383847/is-it-possible-to-modify-the-colmodel-after-data-has-loaded-with-jqgrid/5408195#5408195) can give you the start point of the solution. You still need to decide where you want display the *values* for the attributes. The values can be displayed as additional columns (name, value column pair), as tooltip on the cell with the attribute. Alternatively you can just place the information in **two grids**: master grid with id and name and the second (detail) grid with id, key and values of attributes selected row from the master grid. – Oleg Nov 03 '11 at 12:22
  • That pushed me in the right direction :D And http://stackoverflow.com/questions/1684611/jqgrid-how-to-dynamically-create-columns also nice. – Cine Nov 03 '11 at 14:04

0 Answers0