简体   繁体   中英

Why is grid.Rows.Count always 0?

I have DataSet which is not null. It have 16 rows,

在此处输入图片说明

but my grid have no rows. I have bindingSource , here is some code:

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. Any idea why is that happening?

you need to directly assign the data table to 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....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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