Public Class Form1
Private table As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With table
.Columns.Add("Col1", GetType(Decimal))
.Columns.Add("Col2", GetType(String))
End With
For i As Integer = 1 To 5
table.Rows.Add(i, "item " & i.ToString)
Next
Me.DataGridView1.DataSource = table
'Set the cell currency format style to use the minus sign instead of the default parenthesis
Dim ci As New Globalization.CultureInfo("en-US")
ci.NumberFormat.CurrencyNegativePattern = 1
Me.DataGridView1.Columns(0).DefaultCellStyle.FormatProvider = ci
Me.DataGridView1.Columns(0).DefaultCellStyle.Format = "c"
End Sub
Private DGVTextBox As DataGridViewTextBoxEditingControl = Nothing
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
DGVTextBox = DirectCast(e.Control, DataGridViewTextBoxEditingControl)
RemoveHandler DGVTextBox.KeyPress, AddressOf DGVTextBox_KeyPress
AddHandler DGVTextBox.KeyPress, AddressOf DGVTextBox_KeyPress
End If
End Sub
Private Sub DGVTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = "-"c Then
Dim curCell As DataGridViewCell = DataGridView1.CurrentCell
If curCell.ColumnIndex = 0 Then
curCell.Value = -CDec(curCell.Value)
DataGridView1.RefreshEdit()
e.Handled = True
End If
End If
End Sub
End Class