$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
PropertyGrid WinForms Control
Easily process properties of data objects, automatically generate and display custom data in C#.
Home > WinForms UI Controls > User Manual > Validation to PropertyGrid Editors in C#

Data Validation in PropertyGrid

KetticPropertyGrid control for Windows Forms contains a variety of built-in editors used to edit different data types. All the built in editors can be replaced by custom editors. This PropertyGrid control for Windows Forms allows the users easily to validate data before the data is committed. The data validation process will re quire the ValidateProperty event and the data editing mode will be enabled by disabling the read-only mode of the PropertyGrid items.

Handle PropertyGrid Data Validating Event of in C#.NET

KetticPropertyGrid control for Windows Forms application offers the ValidateProperty event for the users to perform the data validating process. This event for data validation will be fired when we change the current PropertyGrid item or when the PropertyGrid item loses its input focus. To cancel this data validation event, we should input a valid editor value or cancel the data validation process.

Data Validation Events for PropertyGrid

To handle the data validation, we can use the C# events as below,
  • ValidateProperty, this event is used to validate data content of the PropertyGrid item and will be fired as an item loses input focus.
  • ValidatedProperty, this event will be fired when the data validating has been finished.

C# code for Data Validating in PropertyGrid

In the following example, we are going to demonstrate how to validating data for a PropertyGrid item using C# code. The data validation is performed on a string property, which forbids empty string input. The ValidateProperty event will check if the input string is empty. When the data value validation fails, the error indicator will appear and this ValidateProperty event will be canceled. When the data value is valid in Edited event, the error indicator will not appear. The C# code snippet gives more details.

void ketticPropertyGrid1_ValidateProperty (object sender, ValidatePropertyEventArgs e)
{
PropertyGridItem item = e.Item as PropertyGridItem;

if (item.PropertyType == typeof(string))
{
if (string.IsNullOrEmpty(e.NewValue.ToString()))
{
item.ErrorMessage = "Empty string are not allowed!";
e.Cancel = true;
}
}
}

void ketticPropertyGrid1_Edited(object sender, PropertyGridItemEditedEventArgs e)
{
PropertyGridItem item = e.Item as PropertyGridItem;
item.ErrorMessage = "";
}
UI Controlsfor Windows Forms
.NET WinForms UI Overview.NET WinForms UI Features.NET WinForms UI GuideC# WinForms UI DesignVB.NET WinForms UI Design
WinForms UI Controls