我有一些代码,当我执行此对象参考未设置为对象实例时显示错误 并且我将在数据网格视图中对每行的单元格[2]的值求和 这是我的代码:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 2)
{
textBox1.Text = CellSum().ToString();
}
}
private double CellSum()
{
try {
double d = 0;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
d = 0;
//Error shows in this line ....
Double.TryParse(dataGridView1.Rows[i].Cells[2].Value.ToString(), out d);
sum = sum+d;
}
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
return sum;
}