Use IDataErrorInfo in RadGridView with lazy-loaded hierarchy

1 Answer 48 Views
GridView
Tibor
Top achievements
Rank 1
Iron
Tibor asked on 14 Aug 2023, 08:21 AM | edited on 14 Aug 2023, 08:21 AM

Hi! 

Showing a row error message and the warning icon can be achieved by implementing the IDataErrorInfo interface in the data source that's used for the grid, but I'd like to do the same for the child-items in the lazy-loaded hierarchy view (i.e. show a row error for the main object if one of the sub items is incorrect, but after expanding the child items also show an error icon for the individual sub-rows that are incorrect), but somehow the grid is ignoring the IDataErrorInfo implemented in the child objects. Is this an intended behaviour? 

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Aug 2023, 10:11 AM

Hello, Tibor,

According to the provided information, it is not clear enough how the lazy-loaded hierarchy setup. Could you please elaborate? Are you using the load-on-demand approach? Or the automatically generated hierarchy?

I have prepared a sample project where the grid's source contains a collection of schools where each school has a collection of students. Here is the complete code snippet: 

        BindingList<Student> collectionOfStudents = new BindingList<Student>();
        BindingList<School> collectionOfSchools = new BindingList<School>();
        public RadForm1()
        {
            InitializeComponent();

            collectionOfStudents.Add(new Student(0, "Peter", "A+"));
            collectionOfStudents.Add(new Student(1, "John", "D-"));
            collectionOfStudents.Add(new Student(2, "Antony", "B+"));
            collectionOfStudents.Add(new Student(3, "David", "A-"));
            collectionOfStudents.Add(new Student(4, "John", "D-"));
              
            collectionOfSchools.Add(new School(1, "Test School", collectionOfStudents));

            this.radGridView1.AutoGenerateHierarchy = true;
            this.radGridView1.DataSource = collectionOfSchools;
            this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.MasterTemplate.Templates[0].AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.TableElement.RowHeaderColumnWidth = 50;
        }

        public class School : System.ComponentModel.INotifyPropertyChanged, IDataErrorInfo
        {
            int m_id;
            string m_name;
            BindingList<Student> m_students;


            public event PropertyChangedEventHandler PropertyChanged;
            public School(int m_id, string m_name, BindingList<Student> students)
            {
                this.m_id = m_id;
                this.m_name = m_name;
                this.m_students = students;
            }
            public int Id
            {
                get
                {
                    return m_id;
                }
                set
                {
                    if (this.m_id != value)
                    {
                        this.m_id = value;
                        OnPropertyChanged("Id");
                    }
                }
            }
            public string Name
            {
                get
                {
                    return m_name;
                }
                set
                {
                    if (this.m_name != value)
                    {
                        this.m_name = value;
                        OnPropertyChanged("Name");
                    }
                }
            }

            public BindingList<Student> Students
            {
                get
                {
                    return m_students;
                }
                set
                {
                    if (this.m_students != value)
                    {
                        this.m_students = value;
                        OnPropertyChanged("Students");
                    }
                }
            }

            public string this[string columnName]
            {
                get
                {
                    if (columnName == "Name" && (this.Name == string.Empty || this.Name == null))
                    {
                        return "This is not a valid school name!";
                    }
                    return string.Empty;
                }
            }

            [Browsable(false)]
            public string Error
            {
                get
                {
                    if (this.Name == string.Empty || this.Name == null)
                    {
                        return "Please enter valid data in this row!";
                    }
                    return string.Empty;
                }
            }
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        } 

        public class Student : System.ComponentModel.INotifyPropertyChanged, IDataErrorInfo
        {
            int m_id;
            string m_name;
            string m_grade;
            public event PropertyChangedEventHandler PropertyChanged;
            public Student(int m_id, string m_name, string m_grade)
            {
                this.m_id = m_id;
                this.m_name = m_name;
                this.m_grade = m_grade;
            }
            public int Id
            {
                get
                {
                    return m_id;
                }
                set
                {
                    if (this.m_id != value)
                    {
                        this.m_id = value;
                        OnPropertyChanged("Id");
                    }
                }
            }
            public string Name
            {
                get
                {
                    return m_name;
                }
                set
                {
                    if (this.m_name != value)
                    {
                        this.m_name = value;
                        OnPropertyChanged("Name");
                    }
                }
            }
            public string Grade
            {
                get
                {
                    return m_grade;
                }
                set
                {
                    if (this.m_grade != value)
                    {
                        this.m_grade = value;
                        OnPropertyChanged("Grade");
                    }
                }
            }

            public string this[string columnName]
            {
                get
                {
                    if (columnName == "Name" && (this.Name == string.Empty || this.Name == null))
                    {
                        return "This is not a valid name!";
                    }
                    return string.Empty;
                }
            }

            [Browsable(false)]
            public string Error
            {
                get
                {
                    if (this.Name == string.Empty || this.Name == null)
                    {
                        return "Please enter valid data in this row!";
                    }
                    return string.Empty;
                }
            }
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }

The errors are indicated for the parent and for the child rows as expected:

I have attached my sample project. Please give it a try and see how it works on your end. Am I missing something? Could you please specify the exact steps how to reproduce the problem? 

However, if you need to indicate the errors in any other situations, note that the CellFormatting event allows you to do it. Please have a look at our Demo application >> GridView >> Manipulate data >> Indicate errors:

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

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