为什么grid.Rows.Count总是0?

时间:2016-03-25 11:41:50

标签: c# winforms datagridview

DataSet不是空的。它有16行,

enter image description here

但我的网格没有行。我有bindingSource,这里有一些代码:

bindingSource.DataSource = ds.Tables[0]; //ds is DataSet
grid.DataSource = bindingSource;

double sum1 = 0;
for (int i = 0; i < grid.Rows.Count; ++i)
{
    sum1 += Convert.ToDouble(grid.Rows[i].Cells[13].Value);
}

当我开始调试时,当涉及到i < grid.Rows.Count时,它就会跳出for循环。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:4)

您需要直接将数据表分配给DataGridView DataSource ..

grid.DataSource = ds.Tables[0];

double sum1 = 0;
for (int i = 0; i < grid.Rows.Count; ++i)
{
    sum1 += Convert.ToDouble(grid.Rows[i].Cells[13].Value);
}

它有效......