【问题标题】:DataGridViewComboBoxCell creates Null Reference ExceptionDataGridViewComboBoxCell 创建空引用异常
【发布时间】:2014-08-06 09:26:12
【问题描述】:

我是 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
    }

【问题讨论】:

    标签: c# winforms datagridview combobox datagridviewcomboboxcell


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      • 2023-03-13
      • 2013-09-21
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多