C# Convert.toDouble()为空值时报错
如题。
#region 实现最后一行合计函数 public void TotalRow(DataGridView dataGridview1) { DataGridViewRow dgr = dataGridview1.Rows[dataGridview1.Rows.Count - 1]; dgr.ReadOnly = true; dgr.DefaultCellStyle.BackColor = System.Drawing.Color.Khaki; dgr.Cells[0].Value = "合计"; double db; for (int i = 0; i < dataGridview1.Rows.Count - 1; i++) { try { db = Convert.ToDouble(dgr.Cells[4].Value) + Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value ??1); dgr.Cells[4].Value = db.ToString("#0.00"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } #endregion
if(!string.isNullOrEmpty(dgr.Cells[4].Value))
{
Convert.ToDouble(dgr.Cells[4].Value)
}
if(!dataGridview1.Rows[i].Cells[4].Value))
{
value2=Convert.ToDouble(dataGridview1.Rows[i].Cells[4].Value ??1
}
db = value1+value2;
dgr.Cells[4].Value = db.ToString("#0.00"); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}