我有一个静态方法来检查给定列的总和。我在事件RowsAdded
上使用它,但我收到以下错误:
System.NullReferenceException was unhandled
Message=Object reference not set to an instance of an object.
这是我的代码,请告诉我我错在哪里
public static decimal datagSum(DataGridView dgv, string colName)
{
int i = dgv.RowCount - 1, j = 0;
decimal dgvSum = 0;
decimal k = 0;
while (j < i)
{
if (dgv.Rows[(i - 1)].Cells.Count > 3)
{
if (decimal.TryParse(dgv.Rows[(i - 1)].Cells[colName].Value.ToString(), out k))
{
dgvSum += k;
}
j++;
}
}
return dgvSum;
}
答案 0 :(得分:0)
代码段
if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName")
{
if (String.IsNullOrEmpty(e.FormattedValue.ToString()))
{
dataGridView1.Rows[e.RowIndex].ErrorText =
"Company Name must not be empty";
e.Cancel = true;
}
}