2012-05-23 60 views
0

我有一個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(); 
    } 
} 
+3

你得到了什麼異常,以及在哪一行? – Bridge

回答

1

這個問題問得不好,不太可能爲您解答您的問題。請閱讀http://tinyurl.com/so-hints以瞭解如何提出編碼問題的一般準則。你對這個問題所做的工作越多,回答你的問題的回答者就越多。隨着迄今所提供的信息量,最好我們可以給你的是,異常可能是因爲你使用的.操作上具有空值的變量。有用的權利;-)

這就是說,你可以採取幾個其他的步驟來清理你的代碼。我們先從第一次嘗試抓住開始。您在許多地方包括dgv_Panchang,包括您的finally塊。你確定dgv_Panchang永遠不會爲空嗎?如果它爲空,則可以獲得NullReferenceException。此外,它看起來像你的兩個catch塊做同樣的事情。你也許可以取下內部的try-catch無需更改任何代碼的語義(堆棧跟蹤可能對捕獲的異常不同的行號)

前進。在許多地方,你會立即設置變量到一個新的對象,然後將其設置爲不同的東西:即

ComboBox cmbBox = new ComboBox(); 
cmbBox = (ComboBox)sender; 

Cls_Global_Var.Name = string.Empty; 
Cls_Global_Var.Name = cmbBox.SelectedItem.ToString(); 

Cls_Global_Var.StrSql = string.Empty; 
Cls_Global_Var.StrSql = "select Pk_SpecialDay from tbl_specialday where V_Title ='" + Cls_Global_Var.Name + "'"; 

第一行可以在所有這些情況中移除,以節省時間和內存。這沒有錯,但僅僅是一個代碼審查建議。

看來您正在使用一些靜態全局類來存儲信息,如Cls_Global_Var.NameCls_Global_Var.StrSql。可能有更好的方法來處理這個問題,而不靜態的,如果你也必須要小心的多線程問題,如果適用,但是這是太大的問題,並且需要比你這裏給出討論什麼更多的上下文。

何時dgv_Panchang.CurrentCell.ColumnIndex == 1和代碼,當它是4非常相似。除額外的try-catch中,正如我前面提到的,可能是不需要的以後,唯一的區別是額外的子句中的if語句:

Cls_Global_Var.Name = cmbBox.SelectedItem.ToString(); 
if (Cls_Global_Var.Name != string.Empty && Cls_Global_Var.Name != null) 

由於.ToString()不會返回NULL, && Cls_Global_Var.Name != null不需要,可以刪除。 (作爲一個側面說明,看看string.IsNullOrEmpty()

一旦你刪除的是,這兩個代碼塊都是同一個意思,你可以將您如果有類似的語句:

if (dgv_Panchang.CurrentCell.ColumnIndex == 1 || dgv_Panchang.CurrentCell.ColumnIndex == 4)

縮短LOC計數像這是一件好事。

還有一個批評:我希望你的comboBox不允許原始的用戶輸入,以免他輸入像"';DROP TABLE tbl_specialday;--"或更糟糕的東西,並毀了你的一天。查看SQL參數以使代碼更健壯。

相關問題