New to Telerik UI for WinForms? Download free 30-day trial

How to Indicate Errors in RadGridView

Environment

Product Version Product Author
2023.2.718 RadGridView for WinForms Desislava Yordanova

Description

RadGridView is one of the most commonly used controls for presenting data and allowing the end users add/update/delete the records. Indicating wrong/incorrect values is very essential for each application that offers data manipulation. This article demonstrates how to show incorrect values entered in the grid's cells.

indicate-errors-in-gridview

Solution

The IDataErrorInfo interface provides the functionality to offer custom error information that a user interface can bind to. If the bound objects to the grid rows implements the IDataErrorInfo interface, once the end user enters an incorrect value and commits the editor's changes, the error icon appears with the appropriate message:

indicate-errors-in-gridview001


        BindingList<Student> collectionOfStudents = new BindingList<Student>(); 
        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-"));


            this.radGridView1.AutoGenerateHierarchy = true;
            this.radGridView1.DataSource = collectionOfStudents;
            this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; 
            this.radGridView1.TableElement.RowHeaderColumnWidth = 50;
        }

        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 "The Name is not valid!";
                    }
                    else if (columnName == "Id" && this.Id < 0)
                    {
                        return "The Id is not valid!";
                    }
                    return string.Empty;
                }
            }

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

You can also use the ContainsErrors property which indicates whether a cell or row contain errors. You should handle the CellFormatting event in this case:


        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            GridDataCellElement cell = e.CellElement as GridDataCellElement;
            if (cell != null)
            {
                if (cell.ContainsErrors)
                {
                    cell.DrawBorder = true;
                    cell.BorderBoxStyle = BorderBoxStyle.SingleBorder;
                    cell.BorderWidth = 2;
                    cell.BorderColor = Color.Red;
                }
                else
                {
                    cell.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderWidthProperty, ValueResetFlags.Local);
                    cell.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local);
                }
            }
        }      

As a result the cell containing a wrong value will be highlighted with a red border:

indicate-errors-in-gridview002

Complete SDK project in C# and VB is available here.