DataGridViewComboBoxCell在未绑定的DataGridView WinForm控件中

时间:2010-10-19 03:01:53

标签: winforms datagridview drop-down-menu

我是DataGridView WinForm控件的新手,只是不喜欢数据绑定。我曾经在100年前使用Spread OCX,并发现它很友好。现在我遇到了一个问题,试着做一些简单的事情:

我有一个有两列的网格: 1)姓名 2)状态

我想循环浏览我的“ChinaVisas”系列,并显示申请人的姓名和申请状态。我想让状态列成为一个下拉列表,允许用户通过在下拉列表中选择一个不同的项来更改值。

这就是我在做什么。我有一种感觉,这不是大多数人编码的数据绑定方式,但在这里你去:

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

这似乎有效,但当用户从下拉列表中选择新的状态值时,会返回以下错误:

---------------------------
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   
---------------------------

为什么?

1 个答案:

答案 0 :(得分:0)

enter code here不要将StatusCode对象添加到Items集合中,如下所示:

 CurrentCell.Items.Add(StatusCode)

添加字符串并查看其是否有效

CurrentCell.Items.Add(StatusCode.ToString)