简体   繁体   中英

How do I avoid columns being added twice for a custom derived DataGridView control in VB.NET?

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

尝试使用DataGridView属性AutoGenerateColumns = False。

You can try to set all the properties in the constructor only if you're in design mode.

Check this discussion: How to tell if .NET code is being run by Visual Studio designer

The following hack works, but is not pretty - I'd prefer a better solution still:

In the constructor:

Public Sub New()
    If Me.DesignMode Then InitializeControl()
End Sub

In the handlecreated event of the control:

Private Sub OrderDataGridView_HandleCreated(sender As Object, e As EventArgs) Handles Me.HandleCreated
    InitializeControl()
End Sub

In intializeControls the (designer generated) columns are removed before being added again, it is not pretty, but I guess the performance impact is minimal?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM