2012-11-19 27 views
0

這是我的代碼:如何設置爲null在RowUpdating(...)字段值

protected void ConfigurationGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     // row's Update button is clicked, but before the GridView control 
     // updates the row 
     DropDownList ddlst = 
      ((DropDownList)ConfigurationGridView.Rows[e.RowIndex].FindControl("SubUnitDropDownList")); 

     if (ddlst != null) 
     { 
      ListItem li = ddlst.SelectedItem; 

      if (li != null) 
      { 
       if (Convert.ToInt32(li.Value) == -1) 
       { 
        // next line is where the problem is 
        // null doesn't change the old value 
        // it does nothing, how to I change this value to null?? 
        e.NewValues["SubUnitId"] = null; 
       } 
       else 
       { 
        // we set the actual value 
        // this works nicely 
        e.NewValues["SubUnitId"] = li.Value; 
       } 
      } 
     } 
    } 

基本上,我有一個字段,通過組合框在獲取其值,第一個值combos表示未分配的/不適用的/未設置的值或該記錄的空值,它在我分配其他值(例如1,2或3)時起作用,當組合變爲-1時我想清除該值,即put空值,但上面的行似乎沒有做任何事情,我試過Nullable<int32>, Int32? value = null, Convert.ToInt32(null)實際上把0而不是null。 當我嘗試System.DBNull.Value時出現錯誤: 'System.DBNull'類型的對象無法轉換爲類型'System.Nullable`1 [System.Int32]'。'。

我在做什麼錯?在此先感謝您的意見。

回答

0

嘗試改變e.NewValues["SubUnitId"] = null;

要:

Nullable<Int32> notSetvalue = null; 
e.NewValues["SubUnitId"] = notSetvalue; 

如果我沒有記錯的話,你的領域需要一個空Int32的類型,但你直接傳遞的DBNull對象。

0

的解決方案竟然是

e.NewValues["SubUnitId"] = ""; 

這很好地工作。

1

也許你可以試試這個:

e.NewValues["SubUnitId"]=System.DBNull.Value 
相關問題