DataGridViewComboBoxCell创建空引用异常
本文关键字:异常 引用 创建 DataGridViewComboBoxCell | 更新日期: 2023-09-27 18:08:45
我是Stack Overflow的新手,所以请原谅任何不恰当的形式/礼仪。谢谢!
编辑:我想我的问题不是关于如何修复NullReference异常,而是如何处理它不正确地"退出"组合框当我点击它的外面。
我有一个问题与我的DataGridViewComboBoxCell设置,我正在使用。首先,我有一个包含3列的datagridview,在用户启用编辑后,用DataGridViewComboBoxCell填充单元格。在每一行中,这3个单元格依赖于前一个单元格中的选定项(第一个ComboBoxCell除外)。我遇到的问题是,如果我点击第一个ComboBox,我让它显示下拉列表,但我实际上没有选择任何东西,我移动到下一个ComboBoxCell并尝试点击它以查看其项目列表,它会停止程序并为"NullReference异常未处理"创建一个错误。此异常出现在应用程序的"static void Main()"下。运行(新MainForm ());
处理更改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
}
我在这样的情况下有同样的问题,什么我发现作为这个问题的解决方案是使用 SelectionChangeCommitted
代替SelectedIndexChanged
事件的组合框