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

data dependent column visibility in gridview hierarchy

4 Answers 229 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 14 Oct 2014, 08:28 PM
Hi,

I have a two level hierarchy in my RadGridview and want to hide a column on the second level grid, on a per row basis, based on a data element on the parent row.

I have attached an example, Based on the value of column3 of the master template, subColumn 4 is either visible or not.

I know how to programmatically set the visibility of the column, the only problem is that i do not have a per master template row granularity of control over the visibility of the second level grid's columns. the column is either visible for all of the second level hierarchy grids or not visible.

Is this a limitation of the gridview? At this point, some general guidance on this matter would be very helpful.

Thanks!

4 Answers, 1 is accepted

Sort by
0
Peter
Top achievements
Rank 1
answered on 14 Oct 2014, 08:31 PM
Also, i am working with winforms. If anything else needs to be clarified, please let me know.
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Oct 2014, 08:54 AM
Hello Peter,

Thank you for writing.

RadGridView uses one common GridViewTemplate for each hierarchical level as this template contains identical columns for all child rows from the specific level. Hence, you can either show, or hide a specific column from the template. It is not possible to display two expanded parent rows where the one will display "SubColumn4" for its child rows and the other one will hide it.

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

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Peter
Top achievements
Rank 1
answered on 17 Oct 2014, 04:20 PM
I suspected as much, thanks for the response.

my solution ended up being to grey the column out.

Just out of curiosity, would it be possible to create two templates, one with the column and one without, and then hide one of the templates on a per row basis. Or does that run into the same limitation?
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Oct 2014, 02:58 PM
Hello Peter,

Thank you for writing back.

You can simulate the desired look by using two GridViewTemplates. When you expand the master row, only the respective child template will be visible. Here is a sample code snippet demonstrating how to show the desired template and hide the other one depending on the master row "Id":
public Form1()
{
    InitializeComponent();
 
    this.radGridView1.ViewCellFormatting += radGridView1_ViewCellFormatting;
 
    List<Car> cars = new List<Car>();
    List<Part> parts = new List<Part>();
 
    for (int i = 0; i <= 10; i++)
    {
        cars.Add(new Car(i, "Car" + i));
 
        for (int j = 0; j < 5; j++)
        {
            parts.Add(new Part(j, "Name" + j, i));
        }
    }
    this.radGridView1.EnableGrouping = false;
    this.radGridView1.AllowAddNewRow = false;
    radGridView1.DataSource = cars;
    radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
    GridViewTemplate firstChildtemplate = new GridViewTemplate();
    firstChildtemplate.DataSource = parts;
    radGridView1.MasterTemplate.Templates.Add(firstChildtemplate);
    firstChildtemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
    relation.ChildTemplate = firstChildtemplate;
    relation.RelationName = "CarsParts";
    relation.ParentColumnNames.Add("Id");
    relation.ChildColumnNames.Add("CarId");
    radGridView1.Relations.Add(relation);
 
    GridViewTemplate secondChildtemplate = new GridViewTemplate();
    secondChildtemplate.DataSource = parts;
    secondChildtemplate.Columns["Id"].IsVisible = false;
    radGridView1.MasterTemplate.Templates.Add(secondChildtemplate);
    secondChildtemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
    GridViewRelation relation2 = new GridViewRelation(radGridView1.MasterTemplate);
    relation2.ChildTemplate = secondChildtemplate;
    relation2.RelationName = "CarsParts2";
    relation2.ParentColumnNames.Add("Id");
    relation2.ChildColumnNames.Add("CarId");
    radGridView1.Relations.Add(relation2);
}
 
int id;
 
private void radGridView1_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
    GridDetailViewCellElement cell = e.CellElement as GridDetailViewCellElement;
    if (cell != null)
    {
        GridViewHierarchyRowInfo hierarchyRow = (GridViewHierarchyRowInfo)((GridViewDetailsRowInfo)cell.RowInfo).Owner;
        for (int i = 0; i < cell.PageViewElement.Items.Count; i++)
        {
            RadPageViewItem item = cell.PageViewElement.Items[i];
            item.Text = "Child Template " + i;
            if (int.TryParse(hierarchyRow.Cells[0].Value + "", out id))
            {
                if (id % 2 == 0)
                {
                    if (i == 0 && i < cell.PageViewElement.Items.Count - 1)
                    {
                        cell.PageViewElement.Items[i + 1].IsSelected = true;
                    }
                }
            }
            item.Visibility = Telerik.WinControls.ElementVisibility.Collapsed;
        }
    }
}
 
public class Car
{
    public int Id { get; set; }
 
    public string Title { get; set; }
 
    public Car(int id, string title)
    {
        this.Id = id;
        this.Title = title;
    }
}
 
public class Part
{
    public int Id { get; set; }
 
    public string PartName { get; set; }
 
    public int CarId { get; set; }
 
    public Part(int id, string name, int carId)
    {
        this.Id = id;
        this.PartName = name;
        this.CarId = carId;
    }
}

I hope this information helps. If you have any additional questions, please let me know.

Regards,
Desislava
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Peter
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or