0
votes

This is a Windows forms program. I have an unbound DataGridView control with four columns; Feature ID as string, Parts as string, Flats as string and ShowOnEst as boolean. The columns were defined in the form designer and the boolean column is, indeed, set to be a checkbox with tristate set to false.

The user selects a product from a list box and this code is executed to populate the grid

grdFeat.Rows.Clear()
For i = 0 To curProduct.lstFeatures.Count - 1
    With (curProduct.lstFeatures(i))
        grdFeat.Rows.Add(.FeatureId, .Parts, .Flats, .ShowOnEst)
    End With
Next

If AllowUserToAddRows is false, everthing is good. Setting AllowUserToAddRows to true, however, results in the following error:

FormatException

Value '' cannot be converted to type 'Boolean'.

The error is NOT triggered in my code. It's displayed in a new tab titled "No Source Available".

It seems pretty clear that the process that adds the new editing row is trying to set the checkbox to a string value, or perhaps NULL. I'm very new (a few months) to vb.net, so I suspect I'm overlooking some simple setting somewhere, but after several hours of trying to find it, I'm starting to feel a little foolish here.

I'd give you a list of things I've tried already, but it's a long one. :) I even tried to go around the issue by adding a new row manually, .add("","","",False), but that too gives the same error, even though I'm telling it what to put in the checkbox.

What am I missing? Also, can someone point me to an explanation of how that new editing row thingy works?

2
Do you handle the DefaultValuesNeeded event ? - Graffito
Yes, I added code for that and CellValueNeeded just as an experiment. The error happened before either event fired. My sense from the documentation is that these events are useful in virtual mode, but it was worth a try. I tried turning virtual mode on, too, but same. - JimmyTheGeek
For problem identification purpose, may you try again the proposed workaround, i.e. " set AllowUserToAddRows to false before populating the DataGridView and set it to true after", but insert "Application.DoEvents()" just before "grdFeat.AllowUserToAddRows=true". Other trial: instead of DoEvents(), do grdFeat.Invalidate(). - Graffito
Thanks for your interest in the problem. I've put this one to bed by setting AllowUserToAddRows = false and handling new rows in code. Added a little complexity, but no more errors - JimmyTheGeek

2 Answers

1
votes

From what you explained, I have no real explanation for the problem.

A workaround may be to set AllowUserToAddRows to false before populating the DataGridView and set it to true after.

0
votes

Found it. Hope it helps someone else...

The rows being added are uncommitted rows. To solve it, capture the row added index, set the current cell and notify that the added row is dirty:

grdFeat.Rows.Clear()
For i = 0 To curProduct.lstFeatures.Count - 1
    With (curProduct.lstFeatures(i))
        Dim p = grdFeat.Rows.Add(.FeatureId, .Parts, .Flats, .ShowOnEst)
    End With
    grdFeat.CurrentCell = grdFeat.Rows(p).Cells(0)
    grdFeat.NotifyCurrentCellDirty(True)
Next

Well, with further experimentation, this isn't a total fix. The problem is still there, but at least now handling the DataError event allows me to do something with it in code. Without these lines, the error occurs before the DataError event is triggered.

Still puzzled.