Click here to Skip to main content
15,879,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 6-Sep-10 8:51am
v2
Comments
Toli Cuturicu 6-Sep-10 16:07pm    
Is the DataGridView data bound or not?
coommark 11-Sep-10 9:50am    
Toli: its bound to a Dataset from a SQL Server 2008 DB

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
 
Share this answer
 
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.
 
Share this answer
 
Comments
coommark 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.
If you have to do this, use the DataGridView.OnCellFormatting event. Something like this:

C#
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! :)
 
Share this answer
 
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 
 
Share this answer
 
v2
Thank you everuone for responding. I tried a modified version of the code sample by Henry, here:

C#
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:

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


Then added this event handler:

C#
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.
 
Share this answer
 
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900