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

Grouped row not updating in hierarchy with BindinList

2 Answers 5 Views
This is a migrated thread and some comments may be shown as answers.
Vicent
Top achievements
Rank 1
Vicent asked on 03 Mar 2020, 05:02 PM

When using GridView in Hierarchy mode with a Template and binding to BindingList for Children:

 

public class Parent
{
    public Parent()
    {
        children = new BindingList<Child2>();
    }
 
    public int Id { get; set; }
 
    public string Prop1 { get; set; }
 
    public BindingList<Child2> children { get; set; }
}
 
 
public class Child2 : INotifyPropertyChanged
{
    public int Id { get; set; }
 
    public string Prop1 { get; set; }
 
    private int _Prop2;
    public int Prop2
    {
        get { return this._Prop2; }
        set
        {
            if (this._Prop2 != value)
            {
                this._Prop2 = value;
                if(PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Prop2"));
            }
        }
    }
 
    public string Prop3 { get; set; }
 
    public event PropertyChangedEventHandler PropertyChanged;
}

 

If there is grouped columns in child template, changing properties is not reflected:

public partial class Form1 : Form
   {
 
       BindingList<Parent> data;
       public Form1()
       {
           InitializeComponent();
           InitGrid();
 
           data = new BindingList<Parent>();
 
           for (int i = 0; i < 3; i++)
           {
               Parent parent = new Parent() { Id = i, Prop1 = "testProp" + i };
               for (int j = 0; j < 4; j++)
               {
                   Child2 child = new Child2() { Id = i * j, Prop1 = "testProp" + i, Prop2 = j % 2, Prop3 = "another" };
                   parent.children.Add(child);
               }
 
               data.Add(parent);
           }
 
           this.radGridView1.DataSource = data;
           this.radGridView1.CellDoubleClick += radGridView1_CellDoubleClick;
 
       }
 
       void radGridView1_CellDoubleClick(object sender, GridViewCellEventArgs e)
       {
           Child2 child = e.Row.DataBoundItem as Child2;
           MessageBox.Show(child.Prop2.ToString());
       }
 
       private void InitGrid()
       {
           this.radGridView1.Columns.Add(CreateColumn("Id"));
           this.radGridView1.Columns.Add(CreateColumn("Prop1"));
 
 
           GridViewTemplate template = new GridViewTemplate();
           template.AllowAddNewRow = false;
           template.AllowDeleteRow = false;
           template.AllowEditRow = false;
           template.AutoGenerateColumns = false;
 
 
           template.Columns.Add(CreateColumn("Id"));
           template.Columns.Add(CreateColumn("Prop1"));
           template.Columns.Add(CreateColumn("Prop2"));
           template.Columns.Add(CreateColumn("Prop3"));
 
 
           GridViewRelation r = new GridViewRelation(this.radGridView1.MasterTemplate, template);
           r.ChildColumnNames.Add("children");
 
           template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
 
           template.EnableFiltering = false;
           template.EnableGrouping = true;
           template.ShowColumnHeaders = true;
           template.ShowRowHeaderColumn = false;
 
 
           this.radGridView1.EnableGrouping = false;
           this.radGridView1.EnableCustomGrouping = false;
 
 
           GroupDescriptor gd = new GroupDescriptor();
           gd.GroupNames.Add("Prop2", ListSortDirection.Ascending);
           gd.GroupNames.Add("Prop3", ListSortDirection.Ascending);
 
 
           template.GroupDescriptors.Add(gd);
 
 
           this.radGridView1.Templates.Add(template);
 
           this.radGridView1.Relations.Add(r);
           this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
       }
 
       GridViewTextBoxColumn CreateColumn(string Field)
       {
           GridViewTextBoxColumn col = new GridViewTextBoxColumn();
           col.Name = Field;
           col.FieldName = Field;
           col.HeaderText = Field;
           return col;
       }
 
       private void button1_Click(object sender, EventArgs e)
       {
           this.data.First().children.First().Prop2 = 1;
       }
   }

When I Click Button1 nothing happens

Is this a bug or am I doing something wrong?

 

 

 

 

 

2 Answers, 1 is accepted

Sort by
0
Vicent
Top achievements
Rank 1
answered on 03 Mar 2020, 06:15 PM

Same project without adding GroupDescriptor to child template works with no problem

 

0
Nadya
Telerik team
answered on 06 Mar 2020, 11:24 AM

Hello Vicent,

The provided code snippet is greatly appreciated. When you change the value in a grouped grid the GroupDescriptor is not automatically reevaluated. This is why you need to do it manually. Please refer to the updated Button_Click event:

private void radButton1_Click(object sender, EventArgs e)
{
    List<GroupDescriptor> groups = new List<GroupDescriptor>();
    groups.AddRange(this.radGridView1.Templates[0].GroupDescriptors);
    this.radGridView1.Templates[0].GroupDescriptors.Clear();

    this.data.First().children.First().Prop2 = 1;
    if (groups.Count > 0)
    {
        this.radGridView1.Templates[0].GroupDescriptors.AddRange(groups);
    }
}

I hope this helps. Do not hesitate to contact us if you have other questions.

Regards,
Nadya
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Asked by
Vicent
Top achievements
Rank 1
Answers by
Vicent
Top achievements
Rank 1
Nadya
Telerik team
Share this question
or