创建动态DataGridViewComboBoxCells

问题描述 投票:3回答:1

所以这是我的情况。我有一个DataGridView,它有两列,我试图设置为DataGridViewComboBoxColumns称为“已接收”和“延期交货”。

这些组合框的重点是创建一个下拉菜单,以选择我的公司收到货物时收到/延期交货的数量。该程序将主要在没有鼠标或键盘的触摸屏设置上运行,这就是我选择使用组合框而不仅仅是要求一般用户输入的原因。

我试图设置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

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

System.ArgumentException:DataGridViewComboBoxCell值无效。

为什么我会收到此错误,我该如何解决?我似乎无法弄清楚我在代码中做错了什么。

vb.net datagridview datagridviewcombobox
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.