VB.NET - Input string is not in a correct format in a datagridview in vb,net

Asked By kughan j on 01-Nov-12 03:44 AM
Hi All,

I have a datagridview in the vb.net application.
In that I have a quantity,Pirce and value column in the grid.
When user enters the quantity in the quantity column then the Value is calculating as Price*Quantity.
If the user enters the double space in the quantity column instead of the value then the system throws the error as input string is not in the correct format.

How to solve this .

Thanks in advance.


Rohan Dave replied to kughan j on 01-Nov-12 08:05 AM
The reason is as you are doing calculation (quantity * price)  that's why it's throwing error when you enter space in either quantity or price.

You have two ways to solve it..

1) Make your Quantity and Price mandatory in datagridview. Also specify the "Number" format only. So user can enter only Numeric valeus. So it will alwayz 0 or some valus

2) Another way is before doing calculation you can check somethin like below..

Dim Qty As Decimal = IIF (string.IsNullOrEmpty( your quantity cell),0, valueof your quantity cell )
Dim Price As Decimal = IIF (string.IsNullOrEmpty( your Price cell),0, valueof your Price cell )

Dim Total As Decimal = Qty * Price