Datagridview rowcount showing 0 even when there is a valid datasource

帅比萌擦擦* 提交于 2019-11-29 16:08:14

I found the issue. I was displaying the grid in a tabbed page that was not selected. Unless the grid is visible, it does not raise the rowadded event (which is weird!) durnig databinding. I selected the tab page before doing the databind, and the rowcount worked.

Use this code instead:

BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = resultDt;

dgResult.DataSource = bindingSource;
flowLayoutPanel.Controls.Add(dgResult); 

var c = dgResult.Rows.Count;

The binding source is what's responsible for syncing your data with the control. You want to use it, rather than trying to assign the table directly to the control.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!