Completed
Last Updated: 27 Nov 2018 16:13 by Dimitar
ADMIN
Hristo
Created on: 23 Oct 2017 13:37
Category: GridView
Type: Bug Report
2
FIX. RadGridView - columns in the inner templates of a hierarchical grid cannot be sorted neither reordered after loading a previously saved layout
How to reproduce: check the code snippet below and the attached video. 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.SetupGrid();

        this.radGridView1.Dock = DockStyle.Fill;
        this.radGridView1.UseScrollbarsInHierarchy = true;
    }

    private void SetupGrid()
    {
        BindingList<Teacher> teachers = new BindingList<Teacher>();
        BindingList<Student> students = new BindingList<Student>();

        for (int i = 1; i <= 2; i++)
        {
            teachers.Add(new Teacher
            {
                TeacherId = i,
                TeacherFirstName = "FirstName " + i,
                TeacherLastName = "FirstName " + i,

            });

            for (int j = 1; j <= 3; j++)
            {
                students.Add(new Student
                {
                    SudentId = j,
                    TeacherId = i,
                    SudentFirstName = "Student " + j,
                    SudentLastName = "LastName " + j,

                });
            }
        }

        this.radGridView1.Templates.Clear();
        this.radGridView1.DataSource = null;

        this.radGridView1.DataSource = teachers;
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

        GridViewTemplate template = new GridViewTemplate();
        template.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        template.DataSource = students;
        this.radGridView1.MasterTemplate.Templates.Add(template);

        GridViewRelation relation = new GridViewRelation(radGridView1.MasterTemplate);
        relation.ChildTemplate = template;
        relation.RelationName = "TeacherStudents";
        relation.ParentColumnNames.Add("TeacherId");
        relation.ChildColumnNames.Add("TeacherId");
        this.radGridView1.Relations.Add(relation);

        this.radGridView1.ChildViewExpanded += RadGridView1_ChildViewExpanded1;
    }

    private void RadGridView1_ChildViewExpanded1(object sender, ChildViewExpandedEventArgs e)
    {
    }

    private void radButton1_Click(object sender, EventArgs e)
    {
        this.radGridView1.SaveLayout("..\\..\\save.xml");
    }

    private void radButton2_Click(object sender, EventArgs e)
    {
        this.radGridView1.LoadLayout("..\\..\\save.xml");
    }
}

public class Teacher
{
    public int TeacherId { get; set; }

    public string TeacherFirstName { get; set; }

    public string TeacherLastName { get; set; }
}

public class Student
{
    public int SudentId { get; set; }

    public int TeacherId { get; set; }

    public string SudentFirstName { get; set; }

    public string SudentLastName { get; set; }
}


Workaround: prevent the child templates from serializing
public class MyRadGridView : RadGridView
{
    public override string ThemeClassName
    {
        get
        {
            return typeof(RadGridView).FullName;
        }
    }

    public override void SaveLayout(string fileName)
    {
        MyGridViewLayoutSerializer ser = new MyGridViewLayoutSerializer(this.XmlSerializationInfo);
        using (XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8))
        {
            writer.Formatting = Formatting.Indented;
            writer.WriteStartElement("RadGridView");
            ser.WriteObjectElement(writer, this);
        }
    }
}

public class MyGridViewLayoutSerializer : GridViewLayoutSerializer
{
    public MyGridViewLayoutSerializer(ComponentXmlSerializationInfo componentSerializationInfo)
        : base(componentSerializationInfo)
    { }

    protected override bool ShouldSerializeValue(object component, PropertyDescriptor property, PropertySerializationMetadata overwriteMetadata)
    {
        if (property.Name == "Templates")
        {
            return false;
        }

        return base.ShouldSerializeValue(component, property, overwriteMetadata);
    }
}
(Total attached files size should be smaller than 20mb. Allowed extensions: .zip, .rar, .jpg, .png, .gif)
0 comments