【问题标题】:DataGridViewComboBoxCell in an unbound DataGridView WinForm control未绑定的 DataGridView WinForm 控件中的 DataGridViewComboBoxCell
【发布时间】:2010-10-19 03:01:52
【问题描述】:

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

为什么?

【问题讨论】:

    标签: winforms datagridview drop-down-menu


    【解决方案1】:

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

     CurrentCell.Items.Add(StatusCode)
    

    添加一个字符串,看看它是否有效

    CurrentCell.Items.Add(StatusCode.ToString)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2013-05-17
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多