You can use the CellValidated event to process data after a successful validation, e.g.
vb.net Code:
Imports System.Globalization Imports System.Runtime.CompilerServices Public Module StringExtensions <Extension> Public Function ToTitle(source As String) As String Return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(source) End Function End ModuleAs for actually formatting the input, what's the point? You already have the values with the correct casing written in your code twice so why not just use hard-coded values that already have the correct case?vb.net Code:
Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating Dim columnIndex = e.ColumnIndex If columnIndex = 0 Then Dim validValues = {"In Process", "Completed", "Cancelled"} Dim cellValue = CStr(e.FormattedValue) If Not validValues.Any(Function(s) String.Equals(cellValue, s, StringComparison.CurrentCultureIgnoreCase)) Then MessageBox.Show("The Status value is incorrect! It must be 'In Process', 'Completed' or 'Canceled'") e.Cancel = True End If End If End Sub Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated Dim columnIndex = e.ColumnIndex If columnIndex = 0 Then Dim cell = DataGridView1(columnIndex, e.RowIndex) Dim cellValue = CStr(cell.Value) cell.Value = cellValue.ToTitle() End If End Sub
That said, why not just use a combo box column for this and have them select the appropriate value with the appropriate casing? It's less typing for the user and avoids any mistyping too.vb.net Code:
Private validValues As String() = {"In Process", "Completed", "Cancelled"} Private Sub DataGridView1_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating Dim columnIndex = e.ColumnIndex If columnIndex = 0 Then Dim cellValue = CStr(e.FormattedValue) If Not validValues.Any(Function(s) String.Equals(cellValue, s, StringComparison.CurrentCultureIgnoreCase)) Then MessageBox.Show("The Status value is incorrect! It must be 'In Process', 'Completed' or 'Canceled'") e.Cancel = True End If End If End Sub Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated Dim columnIndex = e.ColumnIndex If columnIndex = 0 Then Dim cell = DataGridView1(columnIndex, e.RowIndex) Dim cellValue = CStr(cell.Value) cell.Value = validValues.First(Function(s) String.Equals(cellValue, s, StringComparison.CurrentCultureIgnoreCase)) End If End Sub




Reply With Quote
