3

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

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

Form1.Controls.Add(grid);

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.

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

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?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
HydroPowerDeveloper
  • 3,302
  • 7
  • 31
  • 38

1 Answers1

2

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

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

LarsTech
  • 80,625
  • 14
  • 153
  • 225