Link to home
Start Free TrialLog in
Avatar of Adnan
AdnanFlag for Norway

asked on

specified cast is not valid when i delete the decimal amount value from my cell i datagridview

I get error message saying that the specified cast is not valid when i delete the decimal amount value from my cell i datagridview and click in another cell the error message shows upp saying that the specified cast is not valid....

The grid cell contain a decimal value 0.00 , and it happens when i delete value and click in another cell....
if ( row.RowState != DataRowState.Detached)
                    if ((int)row.AccountRow.Type >= 128)
                    {
                        sumRegisteredRight += row.Amount;  //    ERROR ON THIS LINE
                    }
                    else
                    {
                        sumRegisteredLeft += row.Amount;
                    }

Open in new window

Avatar of Adnan
Adnan
Flag of Norway image

ASKER

here is the full function...
 private decimal GetSumRegistered( ref decimal sumRegisteredLeft, ref decimal sumRegisteredRight)
        {
            sumRegisteredRight = 0;
            sumRegisteredLeft = 0;
 
            foreach (DsReconItems.MatchItemRow row in dsReconItems1.MatchItem)
            {
                if ( row.RowState != DataRowState.Detached)
                    if ((int)row.AccountRow.Type >= 128)
                    {
                        sumRegisteredRight += row.Amount;
                    }
                    else
                    {
                        sumRegisteredLeft += row.Amount;
                    }
            }
            return sumRegisteredLeft + sumRegisteredRight;
        }

Open in new window

Avatar of Adnan

ASKER

Specified cast is not valid, when column returns NULL or is empety.....??

HELP PLEEAASSEEE...
Your facing this problem due to nullable and type casting. Please follow the following discussion and solve your problem
http://www.developmentnow.com/g/36_2008_2_0_0_1051275/Nullable-types-giving-error.htm
 
 
Avatar of Adnan

ASKER

here is the correct answer for my prob......thank anyway experts....HUHHHH

 if ( row.RowState != DataRowState.Detached && row["Amount"] != DBNull.Value)
ASKER CERTIFIED SOLUTION
Avatar of Vivek Thangaswamy
Vivek Thangaswamy
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial