Click here to Skip to main content
11,493,937 members (70,946 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# DataGridView
I have a DatagridView in a Windows forms application which is used for account transactions.

I want that whenever user enters a number in the Debit Column, it gets converted to a negetive number (eg 500 to -500) immediately and display the negetive number.

Please, how do I do that? I have tried all day without success.

I am using VS 2008, SQL Server 2008, Typed DataSets, and WinForms UI.

Thank you.
Posted 6-Sep-10 8:04am
Edited 6-Sep-10 9:51am
v2
Comments
Toli Cuturicu at 6-Sep-10 16:07pm
   
Is the DataGridView data bound or not?
coommark at 11-Sep-10 9:50am
   
Toli: its bound to a Dataset from a SQL Server 2008 DB
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In an accounting application a negative number is usually enclosed in parentheses, (500) rather than -500.

This can easily be accomplished by handling one of the events, like RowLeave, SelectionChanged or CellEndEdit, whatever is the most appropriate for your implementation
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

As an accountant of many years standing I feel obliged to tell you that your approach is wrong.

A negative Debit is a Credit.

You do not put a negative number in a column, all numbers are absolute values, the Signed Element is decided by the column it is in.
(Double Entry Accounting 101).

(You can have minus numbers in a column, but these are exclusively Reversals.)


Eg...

You have a record for Bank Balance, credits (ie receipts go in the right hand column), and debits (ie payments go in the left).
Then you have a cost ledger which has the reverse entries.

Thus you make a payment for goods, the payment shows as £300 in the left column of the bank account, and the right column of the cost account.

The only reason you would show minus numbers (ie in Brackets) is when you are showing a Balance Sheet, and have a set of costs in a column.

Double Entry has been around for about 800 years, so there are plenty of websites showing how it works.

Here is an Example[^]

As you see, Debit on the left, credit on the right and all are absolute values.
  Permalink  
Comments
coommark at 11-Sep-10 9:52am
   
Yes I agree with you. It was the way I wanted to do it at first. However the client is coming from Microsoft Dynamics NAV, and in NAV, they use positive and negtive number for Credit and Debits respectively. So the need to make this allowance. Thank you for pointing it out.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

If you have to do this, use the DataGridView.OnCellFormatting event. Something like this:

private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == dgvCustomers.Columns["Debit"].Index)
    {
        e.Value = -(Math.Abs(e.Value));
        // IMPORTANT to do the next line to show that it has been handled.
        e.FormattingApplied = true;
    }
}

I have written this off the top of my head. The methodology is correct but I may have misremembered some of the method/property names.

Good Luck! Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Something like this? I cant test as I don't have a compiler handy...

Private Sub DataGridView_CellFormatting(...) Handles DataGridView.CellFormatting
 
  Select Case Me.DataGridView.Columns(e.ColumnIndex).Name
    Case "Debit"
 
    e.Value = CDbl(e.Value) * -1
       DataGridView.Rows(e.RowIndex).Cells("Debit")..DefaultCellStyle.ForeColor = Color.Red
  End Select 
End Sub 
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Thank you everuone for responding. I tried a modified version of the code sample by Henry, here:

private void generalLedgerEntryDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == generalLedgerEntryDataGridView.Columns["DebitAmount"].Index)
            {
                string enteredValue = e.Value.ToString();
                Decimal valueHolder = 0;
                Decimal.TryParse(enteredValue, out valueHolder);
                e.Value = -Math.Abs(valueHolder);
                e.FormattingApplied = true;
            }
        }

However, when i run the application, and try to open the form, it gives the error "NullReferenceException was unhandled by user code"

Earlier, I had tried this at the Dataset level:

First i override begin edit thus:

public override void BeginInit()
            {
                this.ColumnChanging += GeneralLedgerEntryDataTable_RowChanging;
                            }

Then added this event handler:

private void GeneralLedgerEntryDataTable_RowChanging(object sender, DataColumnChangeEventArgs e)
            {
                if (e.Column.ColumnName == "DebitAmount")
                {
                    if (Convert.ToDecimal(e.ProposedValue) == null)
                    {
                        return;
                    }
 
                    else if (Convert.ToDecimal(e.ProposedValue) != 0)
                    {
                        Decimal negetive = 0;
                        string value = e.ProposedValue.ToString();
                        Decimal.TryParse(value, out negetive);
                        e.ProposedValue = -negetive;
                    }
                }
            }


This works fine if i am adding only records that have values in the DebitAmount column. And saves just fine to the database. But as soon as i add a row with empty value in the DebitAmount field, when i try to save to the database, it throws the following exception:InvalidCastException. Object Cannot be cast from DBNull to other type.

I guess what i need is to check if the DebitAmount column is null, and if so, to skip it right? But then i dont know the code to do it!

Please Someone help me with this.

NOTE: I will prefer perfoming this conversion at the Dataset level in the BLL (Its a three layer application) instead of doing so at the UI (datagridview). So i will prefer option two above.

Thanks all for the help.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Hi everything. Thanks I've gotten it. Instead of:

if (Convert.ToDecimal(e.ProposedValue) == null)

I replaced it with:

if (Convert.IsDBNull(e.ProposedValue))

Works neet! thanks for your replies!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 408
1 F-ES Sitecore 254
2 Nakul Chaudhari 235
3 OriginalGriff 235
4 CPallini 223
0 Sergey Alexandrovich Kryukov 10,237
1 OriginalGriff 8,730
2 Sascha Lefèvre 3,664
3 Maciej Los 3,392
4 Richard Deeming 2,500


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 11 Sep 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100