2017-06-20 62 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

因为这是重复的问题,必须标记为重复,而不是提供相同的答案 –