DGV cell containing a number doesn't like If grd1.Item(2, numRowNo).Value = "" Then

J Trahair

Well-known member
Joined
May 14, 2008
Messages
175
Location
Spain
Programming Experience
10+
DGV cell containing a number doesn't like If grd1.Item(2, numRowNo).Value = "" Then

Hi.

In many places I need to test whether a cell contains a value, or is blank.

VB.NET:
If grd1.Item(2, numRowNo).Value = "" Then

However, sometimes the value is a number, such a 12.45 or 5038051368669 (a bar code). At these moments the cell decides to throw an exception because it treats the number as a number not a string.

I thought, if I wanted to treat a DGV cell value as a number I should put Val() around the cell value. I also thought that a DGV cell value was a string regardless of what it actually is.

If the cell contains a number, how can I test for a blank? Normally the cell is blank, not zero, or it contains a string of numbers.

Thank you.
 
Have you tried using something like

VB.NET:
If String.IsNullOrEmpty(TryCast(grdl.Item(2, numRowNo).Value, String)) Then

This will cast the cell value to a string and then check whether it is null or empty. Since the DataGridViewCell.Value property is of type Object, it is possible that it is inferring it's type based on the value in the cell.
 
Are you saying that the cell sometimes contains a String and sometimes a numeric type, or are you just assuming that a blank cell means an empty String? Is this data from a database or are you adding the data manually?
 
I have a small dgv for a shop, the columns are: Barcode/Item Code, Description, Quantity, Price each. The user scans in a few items, there are always one or two unused rows below the latest item.
After each new item or change of quantity, price etc., the following code runs to update the current total:
VB.NET:
      Dim numRowNo As Integer

        'Tot up sub-total and display:
        numRowNo = 0
        gnumTotalBasketPrice = 0
        Do Until numRowNo > grd1.RowCount - 1
            gnumTotalBasketPrice = gnumTotalBasketPrice + (Val(grd1.Item(5, numRowNo).Value) * Val(grd1.Item(8, numRowNo).Value)) * (1 - (Val(grd1.Item(7, numRowNo).Value) / 100))
            If grd1.Item(2, numRowNo).Value = "" Then
                Exit Do
            End If
            numRowNo = numRowNo + 1
        Loop
You can see that when grd1.Item(2, numRowNo).Value doesn't contain a value, the loop exits because there are no more items to calculate.
The problem is that column 2 is the barcode, and might contain 9771362343098 which is a number and therefore can't equal a "", so this line fails when cell 2 contains a number. A previous reply (see above) involved String.IsNullOrEmpty(TryCast(grd1.Item(2, numRowNo).Value, String)) but this returned a True whether the cell was populated or not.
I could use
VB.NET:
If Val(grd1.Item(2, numRowNo).Value) = 0 Then
but it is conceivable that there might be an item code of 0, or even 0000 as a string, which means that the Val() idea isn't reliable. (I have a large number of user-created barcodes which have a value of zero ranging from 000 to ANYALPHATEXT.)
Ideally, I would be able to use the same trigger for exiting a DGV loop such as this, for any other set of values in this or any other application where going through a DGV to gather information etc. is needed.
Thanks for your replies!
 
Would it not make sense to actually check the type of the data then? E.g.
If TypeOf myCell.Value Is Integer Then
    'The cell contains an Integer.
ElseIf TypeOf myCell.Value Is String Then
    'The cell contains a String.
End If
 
Back
Top