C# 即使我有一个数据源,Datagridview行也是0
我正在以编程方式创建datagrid视图并将数据绑定到它。由于某种原因,行是空的。看起来很奇怪,但我不想显示Datagrid视图,只想将其传递到一个类中,该类将数据提取为Excel和CSVC# 即使我有一个数据源,Datagridview行也是0,c#,.net,C#,.net,我正在以编程方式创建datagrid视图并将数据绑定到它。由于某种原因,行是空的。看起来很奇怪,但我不想显示Datagrid视图,只想将其传递到一个类中,该类将数据提取为Excel和CSV this.DataGridView = new System.Windows.Forms.DataGridView(); System.Windows.Forms.BindingSource bindingSource = new System.Windows.Forms.BindingSource(
this.DataGridView = new System.Windows.Forms.DataGridView();
System.Windows.Forms.BindingSource bindingSource = new System.Windows.Forms.BindingSource();
bindingSource.DataSource = DataSource;
this.DataGridView.AutoGenerateColumns = autoGenerateColumns;
this.DataGridView.DataSource = bindingSource;
Int32 rowCount = this.DataGridView.Rows.Count; // 0!
在创建数据源之后,是否有任何方法可以获得要填充的行
谢谢我想知道为什么您需要一个DataGridView而不首先在UI上显示它。我可以推荐几个备选方案
我相信DataGridView在实际呈现之前不会绑定/包含任何行 @保罗·阿伯特·瓦·乌斯是对的 您可以移动对DataGridView.DataBindingComplete事件中的行进行计数的代码
要查看数据网格,应将其添加到父控件集合:
DataGridView = new System.Windows.Forms.DataGridView();
Controls.Add(DataGridView);
当前您的网格没有任何父级,无法显示。autoGenerateColumns的值是多少??我相信DataGridView在实际渲染之前不会绑定/包含任何行。autogenerate设置为true@paul.abbott.wa.us这是我的担心!删除Dgv并使用适当的集合类。
DataGridView = new System.Windows.Forms.DataGridView();
Controls.Add(DataGridView);