我有一个DataGridView
一个ComboBoxColumn
。当我单击它,然后移动到同一列中的下一行或前一行,我得到一个Exception
(和我的应用程序崩溃)。GridViewComboBoxColumns得到的NullReferenceException
这里是我的代码,我该如何解决这个问题?
private void cmbBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
// dgv_Panchang.EndEdit();
string SpID = string.Empty;
//ComboBox cmbBox = (ComboBox)sender;
ComboBox cmbBox = new ComboBox();
cmbBox = (ComboBox)sender;
if (cmbBox != null)
{
if (dgv_Panchang.CurrentCell.ColumnIndex == 1)
{
Cls_Global_Var.Name = string.Empty;
Cls_Global_Var.Name = cmbBox.SelectedItem.ToString();
if (Cls_Global_Var.Name != string.Empty)
{
Cls_Global_Var.StrSql = string.Empty;
Cls_Global_Var.StrSql = "select Pk_SpecialDay from tbl_specialday where V_Title ='" + Cls_Global_Var.Name + "'";
SpID = Cls_DataBase.GetIdentCurrentID(Cls_Global_Var.StrSql);
if (SpID != null)
{
int RowIndex = dgv_Panchang.CurrentCell.RowIndex;
int ColIndex = dgv_Panchang.CurrentCell.ColumnIndex;
DataGridViewCell dgvCurrent = dgv_Panchang[ColIndex + 2, RowIndex];
if (dgvCurrent != null)
{
dgv_Panchang.CurrentCell = dgvCurrent;
dgv_Panchang.CurrentRow.Cells["SPDValue"].Value = SpID;
Cls_PanchangMaster_Obj.GetSpecialDayName(Convert.ToInt32(SpID), ColIndex, dgv_Panchang);
}
}
}
else
{
return;
}
}
else if (dgv_Panchang.CurrentCell.ColumnIndex == 4)
{
try
{
Cls_Global_Var.Name = string.Empty;
Cls_Global_Var.Name = cmbBox.SelectedItem.ToString();
if (Cls_Global_Var.Name != string.Empty && Cls_Global_Var.Name != null)
{
Cls_Global_Var.StrSql = string.Empty;
Cls_Global_Var.StrSql = "select Pk_SpecialDay from tbl_specialday where V_Title ='" + Cls_Global_Var.Name + "'";
SpID = Cls_DataBase.GetIdentCurrentID(Cls_Global_Var.StrSql);
if (SpID != null)
{
int RowIndex = dgv_Panchang.CurrentCell.RowIndex;
int ColIndex = dgv_Panchang.CurrentCell.ColumnIndex;
DataGridViewCell dgvCurrent = dgv_Panchang[ColIndex + 2, RowIndex];
if (dgvCurrent != null)
{
dgv_Panchang.CurrentCell = dgvCurrent;
dgv_Panchang.CurrentRow.Cells["SPDValue"].Value = SpID;
Cls_PanchangMaster_Obj.GetSpecialDayName(Convert.ToInt32(SpID), ColIndex, dgv_Panchang);
}
}
}
}
catch (Exception ex)
{
Cls_GlobalMessage.CreatErrorLog(ex.Message.ToString());
}
}
}
}
catch (Exception ex)
{
Cls_GlobalMessage.CreatErrorLog(ex.Message.ToString());
}
finally
{
dgv_Panchang.ClearSelection();
dgv_Panchang.EndEdit();
}
}
你得到了什么异常,以及在哪一行? – Bridge