Datagridview不显示UI上的数据,但代码中有数据(调试)(Datagridview does not show Data on UI but there is Data in Code (Debug))

我需要将DGV从有界的DGV复制到正常的DGV。

原因:我需要在从数据集加载后添加组合框,并且这在bound-DGV上不可行。 所以我试图在新的DGV中复制内容。 任何更好的想法?

我用这个来复制dgv: copy Datagrids

然后我写了这个用一个按钮测试它:

private void button1_Click(object sender, EventArgs e) { dgvExcel2.AutoGenerateColumns = true; dgvExcel2 = CopyDataGridView(dgvExcel); dgvExcel2.Refresh(); dgvExcel2.Show(); dgvExcel2.Update(); }

(更新,显示,刷新只是为了尝试一些,因为它不工作)

当我在UI上运行程序时,dgvExcel2总是空的。 但是当我调试时,我发现数据中有内容。

I need to copy a DGV from a bounded DGV to a normal DGV.

Reason: I need to add combobox-columns after load from Dataset and thats not possible on bound-DGV. So I tried to copy the content in new DGV. Any better ideas??

I used this to copy the dgv: copy Datagrids

Then i wrote this to test it with a button:

private void button1_Click(object sender, EventArgs e) { dgvExcel2.AutoGenerateColumns = true; dgvExcel2 = CopyDataGridView(dgvExcel); dgvExcel2.Refresh(); dgvExcel2.Show(); dgvExcel2.Update(); }

(Update, Show, Refresh just to try something because it doesn' work)

When I run the program on UI the dgvExcel2 is always empty. But when i debug i see that there is content in data.

最满意答案

如果我没记错的话,如果你想要ComboBox功能,就不能将AutoGenerateColumns设置为true。 这需要在DataGridView中作为DataGridViewComboBoxColumn进行预定义。

最佳实践也不是将数据复制到DGV或从DGV复制数据,而是使用DataTables或List<T>在UI后面处理。

请看下面的答案:

DataGridView设置列单元格Combobox

将数据绑定到Windows窗体DataGridView控件

编辑:

在用户界面后面的数据表中处理数据,而不是直接在DGV之间复制行似乎可以解决您的问题。 这将消除对复制DGV方法的需求,让您的生活变得更加轻松。

这些步骤将会:

以DataTable格式获取数据( myData )

实例化一个BindingSource:

BindingSource binding = new BindingSource();

将数据表分配给绑定源:

binding.DataSource = myData;

然后将该绑定来源分配给您的相关DGV。

myDataGridView.DataSource = binding;

然后,您可以不通过DGV复制行,而是重新分配绑定,或者为新的DGV创建新的绑定,并将相同的数据表分配给新的绑定。

我希望这更清楚,我已经正确地理解了这个问题。

If I remember correctly you can't have AutoGenerateColumns set to true if you want ComboBox functionality. This needs to be predefined in your DataGridView as a DataGridViewComboBoxColumn.

Also best practices would not be to copy data to and from DGVs, but rather handle this behind UI with DataTables or List<T>.

Please see the following answers:

DataGridView set column cell Combobox

Bind Data to the Windows Forms DataGridView Control

Edit:

Handling your data in a datatable behind the UI as opposed to copying the rows directly between the DGVs seems to resolve your issue. This would remove the need for that Copy DGV method and make your life a lot easier.

The steps would :

Get your data in DataTable format (myData)

instantiate a BindingSource:

BindingSource binding = new BindingSource();

Assign datatable to binding source:

binding.DataSource = myData;

Then assign that binding source to your relevant DGVs.

myDataGridView.DataSource = binding;

Then instead of copying rows across your DGVs you can just reassign your bindings, or create new ones for the new DGV and assign the same datatable to the new binding.

I hope this is clearer and I have understood the question correctly.

更多推荐