This is a migrated thread and some comments may be shown as answers.

Unbound columns

1 Answer 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kim
Top achievements
Rank 1
Kim asked on 29 Aug 2013, 05:22 PM
Here is my scenario, 

I bind my grid with a List of foo. 

foo contains a list of bar, which are custom, user defined fields. Each foo has the identical list of bars, but the "Value" of each bar is different, depending on what the user put in.

In my scenario, foo has a list with 2 bars

bar 1 Fieldname="CustomField1"
bar 1 Value="My Field"

bar 2 Fieldname="CustomField2"
bar 2 Value="My Field 2"

My grid will have 5 columns, ID, Name, Desc, CustomField1, CustomField2.

When I bind, ID, Name, Desc populate with data, but of course, CustomField1 and CustomField2 are empty, which I expect.

What is the best way to get the data into these columns?

I hope this isn't too confusing!

Thanks.

Sample Code;



namespace sample
{
    class foo
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Desc { get; set; }
 
        private List<bar> _customFields;
 
        public foo ()
        {
            _customFields = new List<bar>();
        }
    }
     
    class bar
    {
        public string FieldName { get; set; }
        public string Value { get; set; }
    }
}

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 02 Sep 2013, 01:28 PM
Hello Kim,

Thank you for contacting Telerik Support.

The appropriate solution in this particular case is loading programatically Hierarchical data in grid. I have modified a little your code in order to obtain the result displayed in the attached picture:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        List<foo> listFoo = new List<foo>()
        {
            new foo(1,"foo 1","desc foo 1"),
            new foo(2,"foo 2","desc foo 2"),
            new foo(3,"foo 3","desc foo 3"),
        };
        this.radGridView1.MasterTemplate.DataSource = listFoo;
        this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.F
 
        List<bar> listBar = new List<bar>()
        {
            new bar(1,"bar 1.0", "bar value 1.0"),
            new bar(1,"bar 1.1", "bar value 1.1"),
            new bar(2,"bar 2.0", "bar value 2.0"),
            new bar(1,"bar 1.2", "bar value 1.2"),
            new bar(2,"bar 2.1", "bar value 2.1"),
            new bar(1,"bar 1.3", "bar value 1.3"),
            new bar(3,"bar 3.0", "bar value 3.0"),
        };
         
        GridViewTemplate template = new GridViewTemplate();
        template.DataSource = listBar;
        template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        this.radGridView1.MasterTemplate.Templates.Add(template);
 
        GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
        relation.ChildTemplate = template;
        relation.RelationName = "FooBar";
        relation.ParentColumnNames.Add("ID");
        relation.ChildColumnNames.Add("ParentID");
        radGridView1.Relations.Add(relation);
    }
}
 
class foo
{
    public int ID { get; set; }
 
    public string Name { get; set; }
 
    public string Desc { get; set; }
 
    //private List<bar> _customFields;
 
    //public foo ()
    //{
    //    _customFields = new List<bar>();
    //}
    public foo(int iD, string name, string desc)
    {
        this.ID = iD;
        this.Name = name;
        this.Desc = desc;
    }
}
  
class bar
{
    public int ParentID { get; set; }
 
    public string FieldName { get; set; }
 
    public string Value { get; set; }
 
    public bar(int parentID, string fieldName, string value)
    {
        this.ParentID = parentID;
        this.FieldName = fieldName;
        this.Value = value;
    }
}

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Kim
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or