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

Hierarchy grid child rows do not apply changes to data bound objects.

1 Answer 247 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Randy
Top achievements
Rank 1
Randy asked on 16 Nov 2012, 05:19 PM
I am binding objects to a hierarchy grid in code and when a change is made a child row the change is shown in the grid, however, the underlying data object is not updated.  I can manually apply the new value after edit to the object, but it seems like grid should be doing this for me.

public partial class Form1 : Form
    {
        private BindingList<Node> AllNodes = new BindingList<Node>();
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();
        }
 
        public void LoadData()
        {
            LoadAllNodes();
            grid.DataSource = AllNodes;
            LoadAllObjectColumns(grid.MasterTemplate);
            // Load child template
            GridViewTemplate template = new GridViewTemplate();
            LoadAllObjectColumns(template);
            grid.MasterTemplate.Templates.Add(template);
 
            GridViewRelation r = new GridViewRelation(grid.MasterTemplate, template);
            r.ChildColumnNames.Add("Children");
 
            grid.Relations.Add(r);
        }
 
        public void LoadAllNodes()
        {
            Node p1 = new Node("Parent 1", ItemStatus.None);
            Node n1 = new Node("Child 1", ItemStatus.None);
            Node n2 = new Node("Child 2", ItemStatus.None);
            p1.Children.Add(n1);
            p1.Children.Add(n2);
            AllNodes.Add(p1);
            Node p2 = new Node("Parent 2", ItemStatus.None);
            AllNodes.Add(p2);
        }
 
        public static void LoadAllObjectColumns(GridViewTemplate template)
        {
            template.Columns.Clear();
            template.EnableFiltering = true;
            template.AllowAddNewRow = false;
            template.AutoGenerateColumns = false;
 
            GridViewTextBoxColumn colName = new GridViewTextBoxColumn("Name");
            colName.HeaderText = "Name";
            colName.Name = "Name";
            template.Columns.Add(colName);
 
            GridViewComboBoxColumn colStatus = new GridViewComboBoxColumn("Status");
            colStatus.HeaderText = "Status";
            colStatus.Name = "Status";
            colStatus.DataSource = Enum.GetValues(typeof(ItemStatus)).Cast<ItemStatus>();
            template.Columns.Add(colStatus);
 
            template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }
 
        public enum ItemStatus { None, New, Test, Complete };
 
        public class Node : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            protected void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
 
            private string name;
            private ItemStatus status;
            private BindingList<Node> children;
 
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    if (name != value)
                    {
                        name = value;
                        NotifyPropertyChanged("Name");
                    }
                }
            }
 
            public ItemStatus Status
            {
                get
                {
                    return status;
                }
                set
                {
                    if (status != value)
                    {
                        status = value;
                        NotifyPropertyChanged("Status");
                    }
                }
            }
 
            public BindingList<Node> Children
            {
                get
                {
                    return children;
                }
                set
                {
                    children = value;
                }
            }
 
            public Node(string name, ItemStatus status)
            {
                this.name = name;
                this.status = status;
                children = new BindingList<Node>();
            }
        }
    }


1 Answer, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 21 Nov 2012, 09:40 AM
Hello Randy,

RadGridView in object-relational hierarchy mode does not support CRUD synchronization with inner levels. This behvior is by design and provides better performance and small memory footprint. You can manually change the underlying data in this situation using events. Here is an example:
using System.ComponentModel;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace Lab.Grid
{
    public partial class GridObjectRelationHierarchy : MainForm
    {
        private RadGridView gridView = new RadGridView();
        private Parent parent;
 
        public GridObjectRelationHierarchy()
        {
            InitializeComponent();
 
            gridView.Dock = DockStyle.Fill;
            gridView.Parent = this;
            gridView.BringToFront();
 
            gridView.CellValueChanged += gridView_CellValueChanged;
        }
 
        //edit the child data bound item
        void gridView_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            if (e.Row.Parent is GridViewHierarchyRowInfo)
            {
                Child child = e.Row.DataBoundItem as Child;
                if(child != null && e.Column.Name == "Name")
                {
                    child.ParentName = e.Value.ToString();
                }
            }
        }
 
        protected override void OnLoad(System.Drawing.Size desiredSize)
        {
            base.OnLoad(desiredSize);
 
            BindingList<Parent> messageList = new BindingList<Parent>();
 
            this.gridView.DataSource = messageList;
 
            // Create the Child Template
            GridViewTemplate template = new GridViewTemplate();
            template.Columns.Add("ParentId", "ParentId", "ParentId");
            template.Columns.Add("ParentName", "ParentName", "ParentName");
            this.gridView.Templates.Add(template);
 
            // Set the Relationship between the Parent and Child
            GridViewRelation relation = new GridViewRelation(gridView.MasterTemplate, template);
            relation.RelationName = "Children";
            relation.ChildColumnNames.Add("Children");
            this.gridView.Relations.Add(relation);
 
            parent = new Parent(1, "Name1");
            Child child = new Child(1, "Child1");
            parent.Children.Add(child);
            child = new Child(2, "Child2");
            parent.Children.Add(child);
 
            messageList.Add(parent);
 
            parent = new Parent(2, "Name2");
            messageList.Add(parent);
 
            parent = new Parent(3, "Name3");
            messageList.Add(parent);
 
            parent = new Parent(4, "Name4");
            messageList.Add(parent);
        }
 
        protected override void OnButton1Click()
        {
            //add children in the last parent row
            Child child = new Child(3, "Child3");
            parent.Children.Add(child);
            child = new Child(4, "Child4");
            parent.Children.Add(child);
 
            this.gridView.Templates[0].Refresh();
        }
    }
}

I hope this helps.
 
Kind regards,
Julian Benkov
the Telerik team
Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
Tags
GridView
Asked by
Randy
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Share this question
or