0

我是 Stack Overflow 的新手,所以请原谅任何不当的形式/礼仪。谢谢!

编辑:我想我的问题不是关于如何修复 NullReference 异常,而是当我在组合框外部单击时如何不正确地“退出”组合框。

我正在使用的 DataGridViewComboBoxCell 设置有问题。首先,我有一个包含 3 列的 datagridview,在用户启用编辑时,会使用 DataGridViewComboBoxCell 填充单元格。在每一行中,这 3 个单元格取决于前一个单元格中的选定项(第一个 ComboBoxCell 除外)。我遇到的问题是,如果我单击第一个 ComboBox 并让它显示下拉列表,但我实际上并没有选择任何内容,而是移动到下一个 ComboBoxCell 并尝试单击它以查看它停止的项目列表程序并为“未处理 NullReference 异常”创建错误。此异常出现在 Application.Run(new MainForm()) 的“static void Main()”下;

处理更改 ComboBox 选定索引的代码,该索引应自动填充其他 ComboBox。

private void LoadRules_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {

        DataGridViewComboBoxEditingControl cbx = e.Control as DataGridViewComboBoxEditingControl;
        if (cbx != null)
        {

            if (this.LoadRulesDataGridView.Columns[LoadRulesDataGridView.CurrentCell.ColumnIndex].Name.Equals("OEM"))
            {
                ComboBox cmbprocess = e.Control as ComboBox;


                cmbprocess.SelectedIndexChanged += new EventHandler(OEMBox_SelectedIndexChanged);
                cmbprocess.SelectedIndexChanged += new EventHandler(ModelBox_SelectedIndexChanged);
            }
        }

    }

    private void OEMBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        NetMonDB.DBManager dbConn = new NetMonDB.DBManager(ConnStr, this.LogWarning, this.LogError);
        ComboBox cmbprocess = (ComboBox)sender;

        int row = this.LoadRulesDataGridView.CurrentCell.RowIndex;
        string OEM = cmbprocess.SelectedItem.ToString();
        this.RulesGridModels(row, cmbprocess, dbConn, OEM);//this method gets the required info from the database and loads it into the ComboBox

        cmbprocess.SelectedIndexChanged -= new EventHandler(OEMBox_SelectedIndexChanged);
    }

    private void ModelBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        NetMonDB.DBManager dbConn = new NetMonDB.DBManager(ConnStr, this.LogWarning, this.LogError);
        ComboBox cmbprocess = (ComboBox)sender;

        int row = this.LoadRulesDataGridView.CurrentCell.RowIndex;
        string Model = cmbprocess.SelectedItem.ToString();
        string OEM = cmbprocess.SelectedItem.ToString();
        this.RulesGridOSVersions(row, cmbprocess, dbConn, OEM, Model);//this method gets the required info from the database and loads it into the ComboBox

        cmbprocess.SelectedIndexChanged -= new EventHandler(ModelBox_SelectedIndexChanged);
    }

更新组合框的方法。

private void RulesGridModels(int r, ComboBox comboBox, NetMonDB.DBManager dbConn, string rowOEM)
    {
        //MessageBox.Show(this.LoadRulesDataGridView.Rows[0].Cells[4].Value.ToString());
        DataGridViewComboBoxCell cbo = new DataGridViewComboBoxCell();

        for (int i = 0; i < comboBox.Items.Count; i++)
        {
            cbo.Items.AddRange(comboBox.Items[i]);
        }
        try
        {
            cbo.Items.Clear();
            cbo.Items.AddRange("");
            if (this.LoadRulesDataGridView.Rows[r].Cells[4].Value == null)
                this.LoadRulesDataGridView.Rows[r].Cells[4].Value = "";

            NetMonDB.Phone OEMPhone = dbConn.getOEMId(rowOEM);
            foreach (NetMonDB.Phone phone in dbConn.getModel(OEMPhone))
            {
                cbo.Items.Add(phone.Model);
            }
            this.LoadRulesDataGridView.Rows[r].Cells[5] = cbo;
        }
        catch (Exception e)
        {
            LogError("RulesGridModels", e.ToString());
        }
    }

    private void RulesGridOSVersions(int r, ComboBox comboBox, NetMonDB.DBManager dbConn, string rowOEM, string rowModel)
    {
        DataGridViewComboBoxCell cbo = new DataGridViewComboBoxCell();

        for (int i = 0; i < comboBox.Items.Count; i++)
        {
            cbo.Items.AddRange(comboBox.Items[i]);
        }
        try
        {

            cbo.Items.Clear();
            cbo.Items.AddRange("");
            NetMonDB.Phone CurrentPhone = dbConn.getOEMId(rowOEM);
            CurrentPhone.Model = rowModel;
            foreach (NetMonDB.Phone phone in dbConn.getOSVersion(CurrentPhone))
            {
                cbo.Items.Add(phone.OSVersion);
            }
            this.LoadRulesDataGridView.Rows[r].Cells[6] = cbo;
        }
        catch (Exception e)
        {
            LogError("RulesGridOSVersions", e.ToString());
        }

    }

捕获异常的位置。

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());//Crashes at this Point
    }
4

1 回答 1

3

在这种情况下我遇到了同样的问题,我发现作为这个问题的解决方案是使用 SelectionChangeCommitted组合框而不是SelectedIndexChanged 事件。

于 2014-11-15T05:10:24.597 回答