What are the different validation events and event members in WinForms GridControl?
Different validation events and event members
The WinForms Grid offers validation in three ways:
- Cell-wise Validation
- Character-wise validation
- Grid validation (Cells validation)
To validate the Grid on a cell-by-cell basis, two handlers are offered. They are CurrentCellValidating and CurrentCellValidated handlers.
Cell-wise Validation
CurrentCellValidated: The CurrentCellValidated event is fired only when you try to leave the current cell after editing.
Event Data: The event handler receives an argument of type System.EventArgs.
The following code changes the text color when the cell value is greater than 50 in the GridControl. In the GridDataBoundGrid, the output lines are written based on the above condition. The following is the code example for the GridControl.
C#
void gridControl1_CurrentCellValidated(object sender, EventArgs e) { cc = this.gridControl1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) if (int.Parse(cc.Renderer.ControlValue.ToString()) > 50) this.gridControl1[cc.RowIndex, cc.ColIndex].TextColor = Color.Red; }
VB
Private Sub gridControl1_CurrentCellValidated(ByVal sender As Object, ByVal e As EventArgs) cc = Me.gridControl1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 50 Then Me.gridControl1(cc.RowIndex, cc.ColIndex).TextColor = Color.Red End If End If End Sub
The following screenshot displays the text color when the cell value is greater than 50.
Figure 1: Cell value is greater than 50
The following is the code example for the GridDataBoundGrid.
C#
void gridDataBoundGrid1_CurrentCellValidated(object sender, EventArgs e) { cc = this.gridControl1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) if (int.Parse(cc.Renderer.ControlValue.ToString()) > 50) this.gridControl1[cc.RowIndex, cc.ColIndex].TextColor = Color.Red; }
VB
Private Sub gridDataBoundGrid1_CurrentCellValidated(ByVal sender As Object, ByVal e As EventArgs) cc = Me.gridControl1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 50 Then Me.gridControl1(cc.RowIndex, cc.ColIndex).TextColor = Color.Red Console.WriteLine("value is more than 50") End If End If End Sub
The following screenshot displays the cell value greater than 50.
Figure 2: Cell value is greater than 50
CurrentCellValidating: The CurrentCellValidating is also fired when you try to leave the current cell after editing. It is a cancellable event when the cancel property is set to true, and then the current cell focus can be made to remain in the cell.
Event Data: The event handler receives an argument of type, System.ComponentModel.CancelEventArgs, thus enabling canceling actions.
Cancel - The act of leaving the cell can be canceled by setting Cancel to true.
The following code passes when the second column entries are found to be present or absent, or else it cancels the actions and the current cell focus remains in the same cell.
The following is the code example for the GridControl:
C#
void gridControl1_CurrentCellValidating(object sender, CancelEventArgs e) { cc = this.gridControl1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) if (int.Parse(cc.Renderer.ControlValue.ToString()) > 70) { e.Cancel = true; MessageBox.Show("Value should be less than 70","GridControl"); } }
VB
Private Sub gridControl1_CurrentCellValidating(ByVal sender As Object, ByVal e As CancelEventArgs) cc = Me.gridControl1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 70 Then e.Cancel = True MessageBox.Show("Value should be less than 70","GridControl") End If End If End Sub
The following screenshots display that the focus is not changed in the current cell by setting CurrentCellValidating event e.Cancel=true. The MessageBox is displayed.
.
Figure 3: Value to be less than 70
The following is the code example for the GridDataBoundGrid.
C#
void gridDataBoundGrid1_CurrentCellValidating(object sender, CancelEventArgs e) { cc = this.gridDataBoundGrid1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) if (int.Parse(cc.Renderer.ControlValue.ToString()) > 70) { e.Cancel = true; MessageBox.Show("Value should be less than 70","GridDataBoundGrid"); } }
VB
Private Sub gridDataBoundGrid1_CurrentCellValidating(ByVal sender As Object, ByVal e As CancelEventArgs) cc = Me.gridDataBoundGrid1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 70 Then e.Cancel = True MessageBox.Show("Value should be less than 70","GridDataBoundGrid") End If End If End Sub
Figure 4: Value to be less than 70
Character-wise Validation
There is a special event called the CurrentCellValidateString that does character-wise validation.
CurrentCellValidateString: The CurrentCellValidateString is fired with each key. This examines each key stroke in a cell.
Event Data: The event handler receives an argument of type, Syncfusion.Windows.Forms.Grid.GridCurrentCellValidateStringEventArgs that contains data related to this event. The following GridCurrentCellValidateStringEventArgs properties provide specific information for this event.
Text - The text that is being edited in a cell is stored in the Text property.
Cancel - A key stroke in a cell can be canceled by setting Cancel to true.
The following is the code example for the GridControl.
C#
void gridControl1_CurrentCellValidateString(object sender, GridCurrentCellValidateStringEventArgs e) { Console.WriteLine(e.Text); }
VB
Private Sub gridControl1_CurrentCellValidateString(ByVal sender As Object, ByVal e As GridCurrentCellValidateStringEventArgs) Console.WriteLine(e.Text) End Sub
The following is the code example for the GridDataBoundGrid:
C#
void gridDataBoundGrid1_CurrentCellValidateString(object sender, GridCurrentCellValidateStringEventArgs e) { Console.WriteLine(e.Text); }
VB
Private Sub gridDataBoundGrid1_CurrentCellValidateString(ByVal sender As Object, ByVal e As GridCurrentCellValidateStringEventArgs) Console.WriteLine(e.Text) End Sub
The following screenshots display the CurrentCellValidateString event fired for each key in both the GridControl and GridDataBoundGrid:
Figure 5: Character-wise Validation
Grid Validation
To validate the whole grid, two handlers are offered in common to the GridControl and GridDataBoundControl. They are Validating and Validated handlers. A range of cells can be validated at a time in this manner. There is a special handler, ValidateFailed for the GridDataBoundGrid.
Validated: The Validated event is fired only from another control that has the CausesValidation property set to true.
Event Data: The event handler receives an argument of the type, System.EventArgs.
The following code validates all the entries of column 4 on the click of a button. If entries are found to be less than 45, a message window either shows cannot be saved or saved.
The following is the code example for the GridControl:
C#
void gridControl1_Validated(object sender, EventArgs e) { if(checkBox2.Checked) { int rowIndex = this.gridControl1.Model.RowCount; bool flag = false; try { for (int i = 1; i < rowIndex; i++) // Validating all the entries of 4th Column if (int.Parse(this.gridControl1.Model[i, 4].Text) < 45) flag = true; if (flag) { //Pops up when entries are < 45. MessageBox.Show("Cannot be saved"); flag = false; } else MessageBox.Show("saved"); } catch (System.FormatException exp) { // Pops up when No entries and text are entered. MessageBox.Show("Cannot be saved"); } } }
VB
Private Sub gridControl1_Validated(ByVal sender As Object, ByVal e As EventArgs) If checkBox2.Checked Then Dim rowIndex As Integer = Me.gridControl1.Model.RowCount Dim flag As Boolean = False Try For i As Integer = 1 To rowIndex - 1 ' Validating all the entries of 4th Column. If Integer.Parse(Me.gridControl1.Model(i, 4).Text) < 45 Then flag = True End If Next i If flag Then 'Pops up when entries are < 45. MessageBox.Show("Cannot be saved") flag = False Else MessageBox.Show("saved") End If Catch exp As System.FormatException ' Pops up when No entries and text are entered. MessageBox.Show("Cannot be saved") End Try End If End Sub
The following is the code example for the GridDataBoundGrid:
C#
private void gridDataBoundGrid1_Validated(object sender, System.EventArgs e) { int rowIndex = this.gridDataBoundGrid1.Model.RowCount; bool flag = false; try { for (int i = 1; i < rowIndex; i++) // Validating all the entries of 4th Column. if (int.Parse(this.gridDataBoundGrid1.Model[i, 4].Text) < 45) flag = true; if (flag) { //Pops up when entries are < 45. MessageBox.Show("Cannot be saved GridDataBoundGrid"); flag = false; } else MessageBox.Show("saved"); } catch (System.FormatException exp) { // Pops up when No entries and text are entered. MessageBox.Show("Cannot be saved"); } }
VB
Private Sub gridDataBoundGrid1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Dim rowIndex As Integer = Me.gridDataBoundGrid1.Model.RowCount Dim flag As Boolean = False Try For i As Integer = 1 To rowIndex - 1 ' Validating all the entries of 4th Column If Integer.Parse(Me.gridDataBoundGrid1.Model(i, 4).Text) < 45 Then flag = True End If Next i If flag Then 'Pops up when entries are < 45. MessageBox.Show("Cannot be saved GridDataBoundGrid") flag = False Else MessageBox.Show("saved") End If Catch exp As System.FormatException ' Pops up when No entries and text are entered. MessageBox.Show("Cannot be saved") End Try End Sub
The following screenshot displays the GridControl.Validated and GridDataBoundGridValidated events fired.
Figure 6: GridControl.Validated event fired when the entries in 4th column is less than 45
Validating: The Validating event is fired only from another control that the CausesValidation property sets to true. It is a cancellable event.
Event Data: The event handler receives an argument of the type, System.ComponentModel.CancelEventArgs, thus enabling canceling action.
Cancel - The action in the external control's handler can be canceled by setting the Cancel to true, when the condition fails.
C#
void gridControl1_Validating(object sender, CancelEventArgs e) { cc = this.gridControl1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) if (int.Parse(cc.Renderer.ControlValue.ToString()) > 50) { e.Cancel = true; MessageBox.Show("Value should be less than 50", "GridControl"); } }
VB
Private Sub gridControl1_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) cc = Me.gridControl1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 50 Then e.Cancel = True MessageBox.Show("Value should be less than 50", "GridControl") End If End If End Sub
The following is the code example for the GridDataBoundGrid.
C#
void gridDataBoundGrid1_Validating(object sender, CancelEventArgs e) { cc = this.gridDataBoundGrid1.CurrentCell; if (cc.ColIndex > 0 && cc.RowIndex > 0) //Checks whether the entered value is more than 50 or not. if (int.Parse(cc.Renderer.ControlValue.ToString()) > 50) { e.Cancel = true; MessageBox.Show("Value should be less than 50", "GridDataBoundGrid"); } }
VB
Private Sub gridDataBoundGrid1_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) cc = Me.gridDataBoundGrid1.CurrentCell If cc.ColIndex > 0 AndAlso cc.RowIndex > 0 Then 'Checks whether the entered value is more than 50 or not. If Integer.Parse(cc.Renderer.ControlValue.ToString()) > 50 Then e.Cancel = True MessageBox.Show("Value should be less than 50", "GridDataBoundGrid") End If End If End Sub
Figure 7: Values to be less than 50
ValidateFailed: The ValidateFailed event is triggered only when the validation fails. This is a special event for the GridDataBoundGrid. With the help of this handler, the default validations can be overruled by setting Handled property to true.
Event Data: The event handler receives an argument of the type, Syncfusion.Windows.Forms.Grid.GridValidateFailedEventArgs that contains data related to this event. The following GridValidateFailedEventArgs properties provide specific information for this event.
Handled - The code inside this handler alone is taken into action.
C#
private void gridDataBoundGrid1_ValidateFailed(object sender, Syncfusion.Windows.Forms.Grid.GridValidateFailedEventArgs e) { cc = this.gridDataBoundGrid1.CurrentCell; if (cc.MoveFromColIndex == 1) { MessageBox.Show("Not Valid Values ", "Validation Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); e.Handled = true; } }
VB
Private Sub gridDataBoundGrid1_ValidateFailed(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridValidateFailedEventArgs) cc = Me.gridDataBoundGrid1.CurrentCell If cc.MoveFromColIndex = 1 Then MessageBox.Show("Not Valid Values ", "Validation Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning) e.Handled = True End If End Sub
The following screenshot displays Validation failed event when characters are entered instead of values:
Figure 8: Invalid values
Samples:
Conclusion
I hope you enjoyed learning about what are the different validation events and event members in WinForms GridControl.
You can refer to our WinForms Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!