我有一個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