Results 1 to 9 of 9

Thread: how to add and remove (-) sign from DataGridview cell ?

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    5

    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    5

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    5

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to add and remove (-) sign from DataGridview cell ?

    Please answer the question(s) I asked.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    5

    Re: how to add and remove (-) sign from DataGridview cell ?

    No. I am testing in simple DataGridview. I m not using any datasource

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2012
    Location
    Bangalore
    Posts
    5

    Re: how to add and remove (-) sign from DataGridview cell ?

    Hi
    we can solve this problem like this

    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

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: how to add and remove (-) sign from DataGridview cell ?

    Quote Originally Posted by Chandradev View Post
    Hi
    we can solve this problem like this
    That was my code sample posted in MSDN forum... I'm glad that it helped you
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width