problem with filling a datagrid with code

JohnDW

Well-known member
Joined
Jun 13, 2012
Messages
60
Location
Antwerp, Belgium
Programming Experience
1-3
Dear forummembers,

I have a problem with filling a datagrid (DGV2) in my form.

The code does not do what I desire.

VB.NET:
Dim I As Integer
ItemLoc Dim As Integer = -1
For I = 0 To DGV2.Rows.Count - 1

If R.Scannummer Is DGV2.Rows (I). Cells (0). Value Then
"Here I changed the code from:
'If R.Scannummer DGV2.Rows = (I). Cells (0). Value Then
'To: If R.Scannummer Is DGV2.Rows (I). Cells (0). Value Then
"Otherwise I get the message That
"Option strict on disallows operands of the type object for operator ''='. Use the 'Is' operator to test for object identity. But the code 'does not function with IS. Is there a way to use the '=' operator?


'Item found
ItemLoc = I
Exit For

end If
Next

'If item is not found, add it
If ItemLoc = -1 Then
DGV2.Rows.Add (R.Scannummer, R.ProductId, R.Productomschrijving, R.CategorieId, R.Productnaam, R.KleurId, R.MaatId, R.Verkoopprijs, 1, R.Verkoopprijs)
else
'If item is already there increasefontsize its count
Item Dim Count As Long = CLng (DGV2.Rows (ItemLoc). Cells (8). Value)
Item Count = 1
Dim newPrice As Decimal = CDec (R.Verkoopprijs * Item Count)
DGV2.Rows (ItemLoc). Cells (8). Value = Item Count
DGV2.Rows (ItemLoc). Cells (9). Value = newPrice
end If

The problem in this code is the syntax of
If R.Scannummer Is DGV2.Rows (I). Cells (0). Value Then
It must be chanched in
If R.Scannummer = DGV2.Rows (I). Cells (0). Value Then

but this doesn't match with the option strict

the message I get than is :
"Option strict on disallows operands of the type object for operator ''='. Use the 'Is' operator to test for object identity. But the code 'does not function with IS. Is there a way to use the '=' operator?"

Any suggestions?

Grtz,

John
 
Last edited:
The Value of a DataGridViewCell is type Object because it has to be able to contain any type of object. If it actually contains a Boolean in this case then you need to cast it as type Boolean so that the compiler knows its actual type. That is all Option Strict On means: you must explicitly cast and convert everything to the actual type required and not expect the system to do it for you at run time. You're already doing just that for a Longs and Decimals in the code you posted so do the same for the Boolean.
 
I've test it and it works when I copy/paist data in the text field.
I copy a scannumber from the table and I paist it in the text field, and the code
fills in the datagrid with the recorddata.
When I use a usb-scanner it doesn't work. I have to put the capslock on with shift key , the data comes in the textfield (the scannumber), but the code doesn't get executed, it will not run.

How does it come?
Is there a way to get it, like an 'execute' after it is inserted?

If I don't put the shift lock on, the data in the text box is like:
çè!à'èà(àééé!
and not like 9780470502228 (that's with the shift lock on).

Please some help.

Txs,

John
 
Back
Top