0

我在页面加载事件中填充我的组合框,如下所示。

try
        {
            List<PreviousVersionData> listID = PreviousVersionData.getDatabase();
            if (listID != null)
            {
                foreach (PreviousVersionData l in listID)
                {
                    cmboBoxPreviousVersion.Items.Add(l.FormatID.ToString() + " - " + l.FormatName);

                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

该部分运行完美,但我想访问数据库以获取其余相应字段,以便对其进行编辑。我想你可以正确的方法来获取基于 selectedvalue 的数据。所选值对每个值都返回 null。我还为字段填充事件提供了我的代码。关于为什么我得到一个空值以及如何纠正这个问题的任何想法。也随时要求澄清或更多信息。

if (cmboBoxPreviousVersion.SelectedValue != null)
        {
            PreviousVersionData pvdata = new PreviousVersionData();
            pvdata = pvdata.getDataByID(cmboBoxPreviousVersion.SelectedValue.ToString());

            Item.FormatID = pvdata.FormatID;
            Item.FormatName = pvdata.FormatName;
            Item.FormatDescription = pvdata.FormatDescription;
            Item.StockID = pvdata.StockID;
            Item.PrintPlantCode = (bool)pvdata.PrintPlantCode;
            Item.PrintWeight = (bool)pvdata.PrintWeight;
            Item.PrintPrice = (bool)pvdata.PrintPrice;

            rChkBoxPlantCode.Checked = Item.PrintPlantCode;
            rChkBoxPrintPrice.Checked = Item.PrintPrice;
            rChkBoxWeight.Checked = Item.PrintWeight;
            cmboBoxStock.Items.Add(Item.StockID);
            rTxtBoxDescription.Text = Item.FormatDescription;
        }

谢谢你。

4

1 回答 1

1

尝试使用:

cmboBoxPreviousVersion.SelectedItem

或者你也可以使用:

cmboBoxPreviousVersion.SelectedIndex

返回项目的索引。

于 2013-11-04T15:21:41.860 回答