【问题标题】:Creating Dynamic DataGridViewComboBoxCells创建动态 DataGridViewComboBoxCells
【发布时间】:2019-06-08 18:20:42
【问题描述】:

这就是我的情况。我有一个 DataGridView,它有两列我试图设置为 DataGridViewComboBoxColumns,分别称为“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 中,用于创建表单时。我正在尝试将数据加载到组合框中,如下所示:

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

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

System.ArgumentException:DataGridViewComboBoxCell 值无效。

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

【问题讨论】:

    标签: vb.net datagridview datagridviewcombobox


    【解决方案1】:

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

    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
    

    【讨论】:

    • 非常感谢。我不敢相信我的问题的解决方法如此简单>_
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 2018-06-28
    • 2013-09-07
    • 2012-03-13
    • 2012-05-03
    • 2011-10-17
    相关资源
    最近更新 更多