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

Set columns for child grid

7 Answers 175 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sonya L
Top achievements
Rank 1
Sonya L asked on 02 Nov 2010, 09:45 PM
Hello,
What would be the best way to programatically define the columns in a ChildGridViewTemplate, when I don't want to auot-generate them from the database?  I have a hierarchical grid set up with two levels, and I would like to define the columns programatically, but when I set AutoGenerateColumns to false and try to add columns to the collection, nothing shows up in the child grid, or the parent grid shows no children.  What is the correct way to do this (should the code be in a particular event of the grid)?  Thanks!

7 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Nov 2010, 08:55 AM
Hello,

In order to do this, you have to define the column in the child template, please take a look at the following example on how to do this:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
public partial class Form1 : Form
{
    List<Person> People = new List<Person>();
    List<Car> Cars = new List<Car>();
    List<Guid> guids = new List<Guid>() { Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid(), Guid.NewGuid() };
    public Form1()
    {
        InitializeComponent();
        this.radGridView1.Dock = DockStyle.Fill;
        this.radGridView1.ReadOnly = false;
        this.radGridView1.AllowAddNewRow = true;
 
        this.radGridView1.AutoGenerateHierarchy = true;
        // load primary data source
        People.Add(new Person(new Guid(guids[0].ToByteArray()), new Guid(guids[2].ToByteArray()), "Bob", 42));
        People.Add(new Person(new Guid(guids[1].ToByteArray()), new Guid(guids[2].ToByteArray()), "Rob", 88));
        People.Add(new Person(new Guid(guids[2].ToByteArray()), new Guid(guids[2].ToByteArray()), "Eric", 22));
        this.radGridView1.DataSource = People;
        this.radGridView1.BestFitColumns();
 
        // Now create first relation
        GridViewTemplate carTemplate = new GridViewTemplate();
        Cars.Add(new Car(guids[2], "Focus", "Ford"));
        Cars.Add(new Car(guids[2], "M3", "BMW"));
        Cars.Add(new Car(guids[2], "R6", "Mazda"));
        Cars.Add(new Car(guids[2], "M1", "Merc"));
        Cars.Add(new Car(guids[2], "H1", "Honda"));
        carTemplate.AutoGenerateColumns = false;
        carTemplate.Columns.Add(new GridViewTextBoxColumn("Id"));
        carTemplate.Columns.Add(new GridViewTextBoxColumn("Model"));
        carTemplate.DataSource = Cars;
 
        GridViewRelation carsRelation = new GridViewRelation(this.radGridView1.MasterTemplate);
        carsRelation.ChildTemplate = carTemplate;
        carsRelation.RelationName = "ParentChild";
        carsRelation.ParentColumnNames.Add("ID2");
        carsRelation.ChildColumnNames.Add("ID");
        this.radGridView1.Relations.Add(carsRelation);
        carTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        carTemplate.BestFitColumns();
        carTemplate.Caption = "Cars";
 
        this.radGridView1.MasterTemplate.Templates.Add(carTemplate);
        radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    }
}
 
public class Person
{
    public Guid ID
    {
        get;
        set;
    }
 
    public Guid ID2
    {
        get;
        set;
    }
 
    public string Name
    {
        get;
        set;
    }
 
    public int Age
    {
        get;
        set;
    }
 
    public Person(Guid id, Guid id2, string name, int age)
    {
        ID = id;
        ID2 = id2;
        Name = name;
        Age = age;
    }
}
 
public class Car
{
    public Guid ID
    {
        get;
        set;
    }
 
    public string Model
    {
        get;
        set;
    }
 
    public string Make
    {
        get;
        set;
    }
 
    public Car(Guid id, string model, string make)
    {
        ID = id;
        Model = model;
        Make = make;
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Sonya L
Top achievements
Rank 1
answered on 03 Nov 2010, 06:56 PM
Emanuel,
Thanks for the response!  When I do this in my code, there is no child table.  I can see the rows in the parent table, but there are no child rows.  If I remove the below code, I can see the child rows, but obviously my custom columns are not there:

gridViewChildTemplate.AutoGenerateColumns =

False

 

gridViewChildTemplate.Columns.Add(

New GridViewTextBoxColumn("NAME"))

Is there another way?  Thanks!

 

0
Sonya L
Top achievements
Rank 1
answered on 03 Nov 2010, 07:01 PM
After further testing, I think I see the problem.  If I don't include the child and parent fields for the relation in the grid, the children don't show up.  I guess I have to include those, but just make them not visible to the user.
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Nov 2010, 10:52 PM
Hello,

You need to have those columns, but you can set the width and maxwidth of those columns to 1, and they won't be visible. Do not set visible to false because that will cause the same behavior you are experiencing...

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Sonya L
Top achievements
Rank 1
answered on 03 Nov 2010, 10:58 PM
Thanks for that tip.  I am expanding the grids in the RowFormatting event, and I see all parent and child rows, but the + expander field is missing.  I made sure that the relation fields were visible, but I still don't see the expand/collapse field.  Is there something else I should check for?
0
Emanuel Varga
Top achievements
Rank 1
answered on 03 Nov 2010, 11:02 PM
Hello again,

This is usually caused by a few things:
1 - some problems in the relation, columns missing.
2 - You forgot to add the child template to the master template, this line
this.radGridView1.MasterTemplate.Templates.Add(carTemplate);

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Alexander
Telerik team
answered on 05 Nov 2010, 04:08 PM
Hello Sonya,

I tried expanding the parent rows in the RowFormatting event of hierarchical RadGridView. I used Emanuel's example given above with only one difference concerning the child row ids to make them related to different parent rows:
People.Add(new Person(new Guid(guids[0].ToByteArray()), new Guid(guids[0].ToByteArray()), "Bob", 42));
People.Add(new Person(new Guid(guids[1].ToByteArray()), new Guid(guids[1].ToByteArray()), "Rob", 88));
People.Add(new Person(new Guid(guids[2].ToByteArray()), new Guid(guids[2].ToByteArray()), "Eric", 22));
 
Cars.Add(new Car(guids[0], "Focus", "Ford"));
Cars.Add(new Car(guids[1], "M3", "BMW"));
Cars.Add(new Car(guids[1], "R6", "Mazda"));
Cars.Add(new Car(guids[2], "M1", "Merc"));
Cars.Add(new Car(guids[2], "H1", "Honda"));

Then, I handled the RowFormatting event to expand the parent rows of the control:
private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
{
    GridViewHierarchyRowInfo row = e.RowElement.RowInfo as GridViewHierarchyRowInfo;
    if (row != null)
    {
        row.IsExpanded = true;
    }
}

In this sample the GridExpanderItem-s are displayed correctly. Please send us more details for your scenario so we can investigate it further.

Best regards,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Sonya L
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Sonya L
Top achievements
Rank 1
Alexander
Telerik team
Share this question
or