简体   繁体   English

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

[英]Why is grid.Rows.Count always 0?

I have DataSet which is not null. 我有不为null的DataSet It have 16 rows, 它有16行,

在此处输入图片说明

but my grid have no rows. 但我的网格没有行。 I have bindingSource , here is some code: 我有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);
}

When I start debugging, when it comes to i < grid.Rows.Count it just jump out of for loop. 当我开始调试时,涉及到i < grid.Rows.Count它只是跳出for循环。 Any idea why is that happening? 知道为什么会这样吗?

you need to directly assign the data table to DataGridView DataSource .. 您需要将数据表直接分配给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);
}

It Works.... 有用....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM