强制绑定的DataGridView来生成列和行绑定、DataGridView

2023-09-03 06:21:19 作者:三分书生气七分流氓范

我编程方式创建一个DataGridView对象,然后使用网格的DataSource属性绑定到数据表中。

I am programmatically creating a DataGridView object and then binding it to a DataTable using the grid's DataSource property.

在创建网格后,我将它添加到一个WinForms的控件集合,即:

After creating the grid, I am adding it to a WinForms's control collection, ie:

Form1.Controls.Add(grid);

我想申请将它传递给Forms.Controls集合之前格式化​​的网格(即改变列标题字幕等)。问题是,在DataGridView的行和列不创建,直到它被添加到窗体。

I want to apply formatting to the grid (i.e. change column header captions etc.) before passing it to the Forms.Controls collection. The problem is that the DataGridView's Rows and Columns are not created until it is added to the form.

有没有办法迫使DataGridView中创建的行和列?

Is there a way to force the DataGridView to create the rows and columns?

我大概可以设置DataGridView.Visible =假,网格添加到窗体,格式化的网格,然后设置DataGridView.Visible =真......可是,我真的需要做的一切我格式化之前我把它添加到窗体的控件集合。任何想法?

I could probably set DataGridView.Visible = false, add the grid to the form, format the grid, and then set DataGridView.Visible = true...HOWEVER, I really need to do all my formatting BEFORE I add it to the form's control collection. Any ideas?

推荐答案

简单的回答是否定的,在DataGridView在窗体的控件集合存在。

The quick answer is no, the DataGridView has to exist in the form's control collection.

您注意到有关设置控制为false,然后格式化,然后将其设置为可见的可能是干净的黑客提供给你。

Your note about setting the control to false, then formatting it, then setting it to visible is probably the "cleanest" hack available to you.