2017-06-20 63 views
1

我想在c#中做方法計算,但是當我運行代碼時,給我轉換值的錯誤。消息錯誤是「對象不能從DBNull轉換爲其他類型」。該錯誤從行int bal = 0開始;在計算轉換

private void btn_total_Click(object sender, EventArgs e) 
    { 

     //int[] columnData = (from DataGridViewRow row in dataGridView3.Rows where row.Cells[2].FormattedValue.ToString() != string.Empty select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray(); 
     //lbl_sum.Text = columnData.Sum().ToString(); 

     int sum = 0; 
     for (int i = 0; i < dataGridView3.Rows.Count; ++i) 
     { 
      sum += Convert.ToInt32(dataGridView3.Rows[i].Cells[2].Value); 
     } 

     int bal = 0; 
     for (int i = 0; i < dataGridView3.Rows.Count; ++i) 
     { 
      bal = Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value) - sum; 
     } 


     if (bal <0) 
     { 
      label_bal_pay.Text = bal.ToString(); 
     } 
     else 
     { 
      lbl_bal.Text = bal.ToString(); 
     } 


     label_bal_pay.Text = bal.ToString(); 
     lbl_bal.Text = bal.ToString(); 
     lbl_sum.Text = sum.ToString(); 
    } 
+1

對該問題添加錯誤的完整描述。如果您嘗試調試您的代碼並瞭解您自己的錯誤,那麼它也不會壞。可能你的錯誤是以dataGridView3.Rows [i] .Cells [3] .Value'的形式給出的,它可以包含非整數的值。 –

+0

我已經編輯了描述。該列中的值是整數。 @ S.Petrosov –

+0

不要在* grid *上執行計算,在實際數據上執行該操作,無論這是數據網格還是對象列表。在這種情況下,您不必解析字符串 –

回答

-1

這意味着你的網格中有空值。 嘗試這樣

private void btn_total_Click(object sender, EventArgs e) 
{ 
    //int[] columnData = (from DataGridViewRow row in dataGridView3.Rows where row.Cells[2].FormattedValue.ToString() != string.Empty select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray(); 
     //lbl_sum.Text = columnData.Sum().ToString(); 

     int sum = 0; 
     for (int i = 0; i < dataGridView3.Rows.Count; ++i) 
     { 
      if (dataGridView3.Rows[i].Cells[2].Value != null && dataGridView3.Rows[i].Cells[2].Value != DbNull.Value) 
       sum += Convert.ToInt32(dataGridView3.Rows[i].Cells[2].Value); 
     } 

     int bal = 0; 
     for (int i = 0; i < dataGridView3.Rows.Count; ++i) 
     { 
      if (dataGridView3.Rows[i].Cells[3].Value != null && dataGridView3.Rows[i].Cells[3].Value != DbNull.Value) 
       bal = Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value) - sum; 
     } 

     if (bal < 0) 
     { 
      label_bal_pay.Text = bal.ToString(); 
     } 
     else 
     { 
      lbl_bal.Text = bal.ToString(); 
     } 


     label_bal_pay.Text = bal.ToString(); 
     lbl_bal.Text = bal.ToString(); 
     lbl_sum.Text = sum.ToString();  
} 

你可以做你空的控制,同時也盡顯你的網格。 如果您使用ADO.NET,則可以使用COALESCE方法。

對於爲例,如果

SELECT column FROM TableName 

查詢可以返回空值,但如果你鍵入

SELECT COALESCE (column, 0) FROM TableName 

這個時候,如果該值爲null,則返回0代替。

+0

您新添加的if語句有三個開放禁忌和一個關閉......我會建議重寫您的建議。 –

0

可能地,dataGridView3.Rows[i].Cells[3].Value中的值是DBNull.Value。 A DBNull.Value無法轉換爲Int32。這意味着表達式Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value)將失敗。

首先測試Value的內容是否爲DBNull.Value。如果是這樣,請跳過它。

if (dataGridView3.Rows[i].Cells[3].Value != DBNull.Value) 
    bal = Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value) - sum; 
+0

這與我提供的重複答案是一樣的。 –

+0

@ S.Petrosov:假設你是一個低調的人:如果這是相同的答案,那麼它必須是一個正確的答案!爲什麼downvote?我確實遵循了從stackoverflow的規則。你在哪裏回答?在評論中?這也很棒,但評論並不意味着答案。此外:我沒有先讀你的評論。 –

+0

因爲這是重複的問題,必須標記爲重複,而不是提供相同的答案 –