views:

30

answers:

1

I am new to the DataGridView WinForm control and just do not like data Binding. I Used to use Spread OCX back 100 years ago and found it friendly. Now I am running into a problem try to do something simple:

I have a grid with two columns: 1) Name 2) Status

I want to loop through my collection of "ChinaVisas" and display the applicant's name and the status of his application. I want to make the status column a drop drop that can allow the user to change the value by selecting a different item in the drop down list.

Here's what I am doing. I have a feeling that this isn't the data binding way that most people would code it, but here ya go:

Private Sub PopulateGridVisa()

    grdVisa.Rows.Add(_Order.ChinaVisas.Count)

    For r As Integer = 0 To _Order.ChinaVisas.Count - 1

        Dim CurrentChinaVisa As ChinaVisa = _Order.ChinaVisas(r)

        For c As Integer = 0 To grdVisa.Columns.Count - 1

            Select Case c

                Case 0
                    Dim CurrentCell As DataGridViewCell = grdVisa.Rows(r).Cells(c)
                    CurrentCell.Value = CurrentChinaVisa.SortName

                Case 1

                    Dim CurrentCell As DataGridViewComboBoxCell = CType(grdVisa.Rows(r).Cells(c), DataGridViewComboBoxCell)

                    For Each StatusCode As StatusCode In _frmMain.ApplicationStartup.StatusCodes
                        If StatusCode.StatusCodeId >= StatusCodeEnum.WaitingToReceive Then
                            CurrentCell.Items.Add(StatusCode)
                        End If
                        If StatusCode.StatusCodeId = CurrentChinaVisa.StatusCodeId Then
                            CurrentCell.Value = StatusCode
                        End If
                    Next


            End Select

        Next

    Next

End Sub

That seems to work, but when the user selects a new status value from the drop down, the following error is returned:

---------------------------
DataGridView Default Error Dialog
---------------------------
The following exception occurred in the DataGridView:



System.ArgumentException: DataGridViewComboBoxCell value is not valid.



To replace this default dialog please handle the DataError event.
---------------------------
OK   
---------------------------

Why?

+1  A: 

enter code hereDon't add the StatusCode object to the Items collection as shown here:

 CurrentCell.Items.Add(StatusCode)

Add a String and see if it works

CurrentCell.Items.Add(StatusCode.ToString)

Chad