I want to have a custom property to a custom class based on the datagridviewcolumn class. I want a property that is a list of datagridvewcolumns and I want to be able to select the columns at design time. What would be a good way to acheive this? I feel like I am making this more complicated than it needs to be.
Here is what I have done so far.
I can successfully make a property that is a single datagridviewcolumn that provides a dropdown to select from available columns at design time using a custom objectselectoreditor and overrriding the filltreewithdata function to fill the list with all datagridviewcolumns in the form. However I am having trouble expanding on this to use a property that is a list of columns. The default editor brings up a collection editor that then creates a new datagridviewcolumn entry with all the datagridviewcolumn proroperties instead of just a dropdown to chose from a list.
The closest I have gotten is by creating a custom editor inherited from the collection object editor. The editor then uses a "custom class" with datagridviewcolumn property that uses the objectselectoreditor that then provides the list to select from for each new entry in the collection.
At this point, I receive and error that it cannot cast the "custom class" to datagridviewcolumn. So I tried to override the getitems and setitems functions so the editor would return a list of just the datagridviewcolumn property of the custom class items. At this point I get an error when building the solution that the datagridviewcolumn object is not marked as serializable.
Below is my code:
The Property from the custom column class
''' <summary>
''' A List of specific DGVColumns from the associated Datagridview Control.
''' </summary>
''' <returns></returns>
<Editor(GetType(MyDataGridViewColumnListEditor), GetType(Drawing.Design.UITypeEditor))>
Public Property TestDGVColList As List(Of DataGridViewColumn)
Get
Return lstTestDGVColList
End Get
Set(value As List(Of DataGridViewColumn))
lstTestDGVColList = value
End Set
End Property
The code for the editors
Inherits CollectionEditor
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property CurrentDataGridViewColumn As DataGridViewColumn
Public Sub New()
MyBase.New(type:=GetType(List(Of SelectedDataGridViewColumnItem)))
End Sub
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
CurrentDataGridViewColumn = context.Instance.datagridviewcolumn
Return MyBase.EditValue(context, provider, value)
End Function
Protected Overrides Function GetItems(editValue As Object) As Object()
Dim lst As New List(Of Object)
If editValue IsNot Nothing Then
For Each sdgvci As DataGridViewColumn In editValue
lst.Add(New SelectedDataGridViewColumnItem With {.CurrentDataGridViewColumn = CurrentDataGridViewColumn, .DataGridViewColumn = sdgvci})
Next
Return lst.ToArray
Else
Return MyBase.GetItems(editValue)
End If
End Function
Protected Overrides Function SetItems(editValue As Object, value() As Object) As Object
Dim lst As New List(Of DataGridViewColumn)
For Each sdgvci As SelectedDataGridViewColumnItem In value
lst.Add(sdgvci.DataGridViewColumn)
Next
Return lst
End Function
Protected Overrides Function CreateInstance(itemType As Type) As Object
Dim obj As Object = MyBase.CreateInstance(itemType)
CType(obj, SelectedDataGridViewColumnItem).CurrentDataGridViewColumn = CurrentDataGridViewColumn
Return obj
End Function
Class SelectedDataGridViewColumnItem
<Editor(GetType(MyDataGridViewColumnSelectionEditor), GetType(Drawing.Design.UITypeEditor))>
Public Property DataGridViewColumn As DataGridViewColumn
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property CurrentDataGridViewColumn As DataGridViewColumn
Sub New()
End Sub
End Class
End Class
Public Class MyDataGridViewColumnSelectionEditor
Inherits ObjectSelectorEditor
Protected Overrides Sub FillTreeWithData(ByVal theSel As Selector, ByVal theCtx As ITypeDescriptorContext, ByVal theProvider As IServiceProvider)
MyBase.FillTreeWithData(theSel, theCtx, theProvider)
Dim dgvcType As Type = GetType(DataGridViewColumn)
Try
For Each dgvcol As DataGridViewColumn In CType(theCtx.Instance.CurrentDataGridViewColumn.datagridview, DataGridView).Columns
If dgvcType.IsAssignableFrom(dgvcol.GetType) Then
Try
Dim [aNd] As SelectorNode = New SelectorNode(dgvcol.HeaderText & " - " & dgvcol.Name, dgvcol)
theSel.Nodes.Add([aNd])
theSel.Sort()
Catch ex As Exception
End Try
End If
Next
Catch ex As Exception
End Try
End Sub
End Class
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…