我是Stack Overflow的新手,所以请原谅任何不正确的表格/礼节。谢谢!
编辑:我想我的问题不是关于如何修复NullReference异常,而是如何正确处理它"退出"当我点击它之外的组合框。我正在使用我正在使用的DataGridViewComboBoxCell设置。首先,我有一个包含3列的datagridview,在用户启用编辑后,使用DataGridViewComboBoxCell填充单元格。在每一行中,这3个单元格依赖于前一个单元格中的选定项目(第一个ComboBoxCell除外)。我遇到的问题是,如果我点击第一个ComboBox,我让它显示下拉列表,但我实际上没有选择任何东西,我移动到下一个ComboBoxCell并尝试单击它以查看其项目列表它会停止程序并为&#34生成错误; NullReference异常未处理"。此异常出现在" static void Main()"在Application.Run(new 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);
}
更新ComboBox的方法。
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
}
答案 0 :(得分:3)
在这样的情况下我遇到了同样的问题,我发现作为此问题的解决方案的是使用 SelectionChangeCommitted
代替SelectedIndexChanged
事件进行组合。< / p>