how to add and remove (-) sign from DataGridview cell ?
Hi
I have one datagridview and there some field are there for money dataType. But if user want to add (-) keyword then it should be display like -$10. if again user will click on (-) keyword then it should be remove i.e $10 .
Could tell me, what is the code for this ? i m using vb.net
Regards
Chandradev
Re: how to add and remove (-) sign from DataGridview cell ?
What is that "(-) keyword" that you're talking about? Is it a button?
As for toggling the value from neg to pos or vice versa, all you have to do is to negate the value, i.e.
value = -value
Re: how to add and remove (-) sign from DataGridview cell ?
Hi,
Thanks for sending reply and sorry for writing incomplete sentence. It is Minus i.e negative keyword. I have to do in Datagridview cell.
Re: how to add and remove (-) sign from DataGridview cell ?
Is the grid bound or unbound, i.e. have you set the DataSource or not? If it's bound, what is it bound to, particularly that column?
Re: how to add and remove (-) sign from DataGridview cell ?
Thanks for your reply. I have one column for example Empsal and it is editable cell and onkeyup or any other event, if the user want to give negative value i.e(-) then it should come -10 and after pressing (-) then it should be go.
Re: how to add and remove (-) sign from DataGridview cell ?
Please answer the question(s) I asked.
Re: how to add and remove (-) sign from DataGridview cell ?
No. I am testing in simple DataGridview. I m not using any datasource
Re: how to add and remove (-) sign from DataGridview cell ?
Hi
we can solve this problem like this
Quote:
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
Re: how to add and remove (-) sign from DataGridview cell ?
Quote:
Originally Posted by
Chandradev
Hi
we can solve this problem like this
That was my code sample posted in MSDN forum... I'm glad that it helped you :)