The new version of RadGridView is coming in May (as beta) and I am eager to show you some of the new features.

There are many methods to indicate errors in RadGridView. However, until now it didn’t support the most natural and easy way for this - the IDataErrorInfo interface. Things have changed, so let me show you how to use this interface.

ErrorIndication 

As MSDN says,“IDataErrorInfo provides the functionality to offer custom error information that a user interface can bind to.”

Actually, you don't need to know anything about this interface. You can just set the ErrorText property of the desired row or cell:

this.radGridView1.Rows[5].ErrorText = "There is an error!";
this.radGridView1.Rows[5].Cells[2].ErrorText = "Invalid cell value!";
 

RadGridView will automatically display error indication at the beginning of the row and will set tooltips. 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:

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);
        }
    }
}
 

However, when you want to display help information based on some condition, you should implement the IDataErrorInfo. In that case you have to implement only two properties:

public class Employee : IDataErrorInfo
{
    //...
    [Browsable(false)]
    public string Error
    {
        get
        {
            if (!IsEmailValid() || !IsNameValid() || !IsPhoneValid())
            {
                return "Please enter valid data in this row!";
            }
            return string.Empty;
        }
    }
 
    public string this[string columnName]
    {
        get
        {
            if (columnName == "Email" && !IsEmailValid())
            {
                return "This is not a valid email!";
            }
            //... return string.Empty;
        }
    }
    // ...
}

 

I hope you like this feature. If you have any comments or ideas we will be glad to hear them.


About the Author

Nikolay Diyanov

Diyanov is the Product Manager of the Native Mobile UI division at Progress. Delivering outstanding solutions that make developers' lives easier is his passion and the biggest reward in his work. In his spare time, Nikolay enjoys travelling around the world, hiking, sun-bathing and kite-surfing.

Find him on Twitter @n_diyanov or on LinkedIn.

Related Posts

Comments

Comments are disabled in preview mode.