$99 VS $1000+. UI Control for ASP.NET AJAX(contains chart and gridview).
DataGridView WinForms Control
Create custom data grid cells to GirdView in C# for your .NET Windows Forms applications
Home > WinForms UI Controls > User Manual > Grid Editors Data Validation in C#

Validate Data for GridView Component

The Kettic DataGridView allows users of the control easily validate data grid cells when data is committed to the data source. To validate data to grid cells and rows, we need to use the ValidateCell event, which will be raised by the Kettic DataGridView when there are any changes to the current cell or when the grid cell loses input focus like. Before we validate data to grid cell, we need to cancel the ValidateCell event to prevent the users from exiting the cell until entering a valid editor value or cancelling the edit process. Meanwhile, we can use the ValidateRow event in the same way to prevent the user from exiting the current row.

Events for Validating Grid Cell in C#

Kettic DataGridView provides a few events for data validating to grid cells and rows. The following are the complete events for the data validation.
  • ValidateCell, this event will be fired as long as a grid data cell loses input focus, which will enable the validation functionality to the content in the grid cell.
  • ValidatedCell, this event will be fired after the data validating to grid cell has been finished.
  • ValidateRow, this event will be fired when the data validating to grid row is processing.
  • ValidatedRow, this event will be fired after the data validating to grid row has been finished.
  • DataGridViewRowInformation.ErrorText, this property is able to indicate the validation errors when we are validating grid cells. An error indicator will be displayed at the row header when a non empty string is set to this property.

C# code for Data Cell Validation

We are going through the following C# code to analyze the process of validating data in grid cell. The C# code snippet will enable the data cell validation in a textbox column for entering only none empty strings, which will disable the data validation and display the error indicator at the row header.

void ketticGridView1_CellValidating(object sender, Kettic.WinForms.UI.ValidateCellEventArgs e)
{
DataGridViewColumn column = e.Column as DataGridViewColumn;
if (e.Row is DataGridViewRowInformation && column != null && column.Name == "ProductName")
{
if (string.IsNullOrEmpty((string)e.Value) || ((string)e.Value).Trim() == string.Empty)
{
e.Cancel = true;
((DataGridViewRowInformation)e.Row).ErrorText = "Validation error!";
}
else
{
((DataGridViewRowInformation)e.Row).ErrorText = string.Empty;
}
}
}
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