创建动态 DataGridViewComboBoxCells

Creating Dynamic DataGridViewComboBoxCells

这是我的情况。我有一个 DataGridView,它有两列,我试图将它们设置为 DataGridViewComboBoxColumns,分别称为 "Received" 和 "Backordered".

这些组合框的目的是创建一个下拉菜单,显示 select 我公司收到货物时 received/backordered 有多少物品。该程序将主要 运行 在没有鼠标或键盘的触摸屏设置上,这就是为什么我选择使用组合框而不是仅要求一般用户输入的原因。

我正在尝试按如下方式设置 DataGridView:

'Setup of Combo Box Columns
'shipmentData is the name of the DataGridView
Dim receiveCol As New DataGridViewComboBoxColumn()
receiveCol.HeaderText = "Received"
receiveCol.Name = "Received"
shipmentData.Columns.Add(receiveCol)
Dim backorderCol As New DataGridViewComboBoxColumn()
backorderCol.HeaderText = "Backordered"
backorderCol.Name = "Backordered"
shipmentData.Columns.Add(backorderCol)

以上代码在创建表单时的 New() Sub 中。我正在尝试将数据加载到 ComboBoxes 中,如下所示:

Dim rowNum As Integer = 0
For Each op As OrderPart In OrderData.GetPartList()
    If op.AmountOrdered > 0 Then
        shipmentData.Rows.Add()
        shipmentData.Rows(rowNum).Cells("PartNumber").Value = op.PartNumber
        shipmentData.Rows(rowNum).Cells("Description").Value = op.Description
        shipmentData.Rows(rowNUm).Cells("Ordered").Value = op.AmountOrdered
        For it As Integer = 0 To op.AmountOrdered
            CType(shipmentData.Rows(rowNum).Cells("Received"), DataGridViewComboBoxCell).Items.Add(it)
            CType(shipmentData.Rows(rowNum).Cells("Backordered"), DataGridViewComboBoxCell).Items.Add(it)
        Next
        rowNum = rowNum + 1
    End If
Next

现在,当我 运行 代码时,组合框已创建,并且它们的数据已添加。但是,每当我 select 组合框列表中的数据值,然后尝试移动到另一个销售时,我都会收到以下错误:

System.ArgumentException: DataGridViewComboBoxCell value is not valid.

为什么会出现此错误,我该如何解决?我似乎无法弄清楚我的代码做错了什么。

虽然我不知道为什么要在您的保管箱中添加递增数字,但如果您打算这样做,请将您的代码更改为以下内容:

For it As Integer = 0 To op.AmountOrdered
    CType(shipmentData.Rows(rowNum).Cells("Received"), DataGridViewComboBoxCell).Items.Add(it.ToString())
    CType(shipmentData.Rows(rowNum).Cells("Backordered"), DataGridViewComboBoxCell).Items.Add(it.ToString())
Next