How to customize the error message in WinForms GridControl?
Customize the error message
When an invalid data format or data type is entered in WinForms GridControl, an error message is displayed. The error message is like xx is not a valid value for Double. This error message can be customized.
Solution
You can handle the CurrentCellValidating event, validate and set the error message. The following code example gives a customized error message when a value other than double is entered.
C#
void gridControl1_CurrentCellValidating(object sender, CancelEventArgs e) { GridCurrentCell cc = this.gridControl1.CurrentCell; //Set the dataformat to the column 2. if (cc.ColIndex == 2) { string newvalue = cc.Renderer.ControlText; try { double d = double.Parse(newvalue); } catch { //Display the error message. cc.ErrorMessage = "u r entered invalid dataformat"; e.Cancel = true; } } }
VB
Private Sub gridControl1_CurrentCellValidating(ByVal sender As Object, ByVal e As CancelEventArgs) Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell 'Set the dataformat to the column 2. If cc.ColIndex = 2 Then Dim newvalue As String = cc.Renderer.ControlText Try Dim d As Double = Double.Parse(newvalue) Catch 'Display the error message. cc.ErrorMessage = "u r entered invalid dataformat" e.Cancel = True End Try End If End Sub
On applying the properties, the grid looks like the following screenshot.
Figure 1: Custom message when an invalid data is entered
Samples:
C#: Customizing the error message
VB: Customizing the error message
Conclusion
I hope you enjoyed learning about how to customize the error message in WinForms GridControl.
You can refer to our WinForms GridControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.
For current customers, you can check out our WinForms 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 WinForms GridControl and other WinForms components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!