In a an application I am using a range of different reusable specialized Windows Forms controls derived from DataGridView. The columns and their properties are meant to be fixed for these, and I would like to use the control using the Visual Studio designer.
The way I have implemented it, is to simply programmatically set all the properties of the control including the columns in the constructor. this way the control shows up correctly in the designer with the column header specified (but of course no data). However when I run code using the controls, the set of columns is duplicated for some reason. I have tried to add the obvious hack of only adding columns in the constructor if not columns exist, but even that doesn't prevent it.
The only way to prevent it, is to instantiate and add the control to the relevant controls collection programmatically, but again I would really like to be able to use the visual aspect of the VS designer.
What is the best way to prevent this?
Constructor Code of one of the controls
Public Sub New()
Dim Info As PropertyInfo
With Me
Info = .GetType.GetProperty("DoubleBuffered", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
Info.SetValue(Me, True, Nothing)
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
.ColumnHeadersDefaultCellStyle.Font = New Font("Segoe UI", 7)
With .Columns
.Add(ApplicationDataGridView.CreateIconColumn("StatusImage"))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("Id", "Id", 32, DataGridViewColumnSortMode.NotSortable, DataGridViewContentAlignment.MiddleCenter))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("Description", "Description", 200))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("CreatedAt", "Created", 120, DataGridViewColumnSortMode.NotSortable, DataGridViewContentAlignment.MiddleCenter))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("Owner", "Owner", 150))
.Add(ApplicationDataGridView.CreateVariableWidthTextOutputColumn("Status", "Status", 100))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("Progress", "Progress", 50, DataGridViewColumnSortMode.NotSortable, DataGridViewContentAlignment.MiddleCenter))
.Add(ApplicationDataGridView.CreateFixedWidthTextOutputColumn("HandleTo", "Deadline", 120, DataGridViewColumnSortMode.NotSortable, DataGridViewContentAlignment.MiddleCenter))
.Add(ApplicationDataGridView.CreateIconColumn("FilesImage"))
End With
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToResizeRows = False
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize
.Dock = DockStyle.Fill
.Margin = New Padding(0)
.MultiSelect = False
.RowHeadersVisible = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
.BorderStyle = System.Windows.Forms.BorderStyle.None
.BackgroundColor = System.Drawing.SystemColors.Control
.ColumnHeadersVisible = True
.GridColor = Color.Gainsboro
End With
End Sub