Hi Gang,
Help me out here, this is not working. I do not know what I am missing and
where i am going wrong. I realize there is a lot of code in it. The
problem is that the property values for DatatableManager are not remembered
from design time to run time when added to the ucGrid, more over when I add
the ucGrid to a form, the properties are not written in the Initialize
Component section.
TIA
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design ' Custom Designers Require this
NameSpace
Imports System.ComponentModel.Design.Serialization ' Instance Descriptors
Imports System.Globalization ' This name space is required for
Imports System.Drawing.Design ' Designers Are in this NameSpace, You Also
need to a reference to System.Design.Dll
Public Class ucDatagrid
Inherits System.Windows.Forms.UserControl
Private m_datatable As DataTable
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Protected Friend WithEvents dbgData As DataGrid
Protected Friend WithEvents mnuGrid As System.Windows.Forms.ContextMenu
Protected Friend WithEvents m_DatatableManager As
ControlLib.DatatableManager
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(ucDatagrid))
Me.dbgData = New System.Windows.Forms.DataGrid
Me.mnuGrid = New System.Windows.Forms.ContextMenu
Me.m_DatatableManager = New ControlLib.DatatableManager(Me.components)
CType(Me.dbgData, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'dbgData
'
Me.dbgData.DataMember = ""
Me.dbgData.Dock = System.Windows.Forms.DockStyle.Fill
Me.dbgData.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dbgData.Location = New System.Drawing.Point(0, 0)
Me.dbgData.Name = "dbgData"
Me.dbgData.Size = New System.Drawing.Size(296, 240)
Me.dbgData.TabIndex = 0
'
'm_DatatableManager
'
Me.m_DatatableManager.AllowSorting = True
Me.m_DatatableManager.AlternatingBackColor = System.Drawing.Color.Empty
Me.m_DatatableManager.BackColor = System.Drawing.Color.Empty
Me.m_DatatableManager.ColumnHeadersVisible = True
Me.m_DatatableManager.Columns.AddRange(New ControlLib.BaseColumn()
{CType(resources.GetObject("resource"), ControlLib.BaseColumn)})
Me.m_DatatableManager.ForeColor = System.Drawing.Color.Empty
Me.m_DatatableManager.GridLineColor = System.Drawing.Color.Empty
Me.m_DatatableManager.GridLineStyle =
System.Windows.Forms.DataGridLineStyle.None
Me.m_DatatableManager.HeaderBackColor = System.Drawing.Color.Empty
Me.m_DatatableManager.HeaderFont = Nothing
Me.m_DatatableManager.HeaderForeColor = System.Drawing.Color.Empty
Me.m_DatatableManager.LinkColor = System.Drawing.Color.Empty
Me.m_DatatableManager.MappingName = Nothing
Me.m_DatatableManager.PreferredColumnWidth = 0
Me.m_DatatableManager.PreferredRowHeight = 0
Me.m_DatatableManager.ReadOnly = False
Me.m_DatatableManager.RowHeadersVisible = False
Me.m_DatatableManager.RowHeaderWidth = 0
Me.m_DatatableManager.SelectionBackColor = System.Drawing.Color.Empty
Me.m_DatatableManager.SelectionForeColor = System.Drawing.Color.Empty
'
'ucDatagrid
'
Me.Controls.Add(Me.dbgData)
Me.Name = "ucDatagrid"
Me.Size = New System.Drawing.Size(296, 240)
CType(Me.dbgData, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Public WriteOnly Property Datasource() As DataTable
Set(ByVal Value As DataTable)
End Set
End Property
<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
_
Public Property DatatableManager() As DatatableManager
Get
Return m_DatatableManager
End Get
Set(ByVal Value As DatatableManager)
m_DatatableManager = Value
End Set
End Property
End Class
<Serializable(),
TypeConverter(GetType(DatatableManager.DatatableManagerTypeConverter))> _
Public Class DatatableManager
Inherits System.ComponentModel.Component
#Region " Component Designer generated code "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
'Required for Windows.Forms Class Composition Designer support
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
'This call is required by the Component Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
m_Columns = New Columns
End Sub
'Component overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Component Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Component Designer
'It can be modified using the Component Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
#Region " Declarations"
Private m_AllowSorting As Boolean
Private m_AlternatingBackColor As Color
Private m_BackColor As Color
Private m_ColumnHeadersVisible As Boolean
Private m_ForeColor As Color
Private m_GridLineColor As Color
Private m_GridLineStyle As DataGridLineStyle
Private m_HeaderBackColor As Color
Private m_HeaderFont As Font
Private m_HeaderForeColor As Color
Private m_LinkColor As Color
Private m_MappingName As String
Private m_PreferredColumnWidth As Integer
Private m_PreferredRowHeight As Integer
Private m_ReadOnly As Boolean
Private m_RowHeadersVisible As Boolean
Private m_RowHeaderWidth As Integer
Private m_SelectionBackColor As Color
Private m_SelectionForeColor As Color
Private m_Columns As Columns
#End Region
#Region " Properties"
<Description("Indicates whether sorting is allowed on the grid table when
this DataGridTableStyle is used."), _
Category("Usability"), _
DefaultValue(False)> _
Public Property AllowSorting() As Boolean
Get
Return m_AllowSorting
End Get
Set(ByVal Value As Boolean)
m_AllowSorting = Value
End Set
End Property
<Description("Gets or sets the background color of odd-numbered rows of
the grid."), _
Category("Appearance"), _
DefaultValue(KnownColor.Window)> _
Public Property AlternatingBackColor() As Color
Get
Return m_AlternatingBackColor
End Get
Set(ByVal Value As Color)
m_AlternatingBackColor = Value
End Set
End Property
<Description("Gets or sets the background color of even-numbered rows of
the grid."), _
Category("Appearance"), _
DefaultValue(KnownColor.Red)> _
Public Property BackColor() As Color
Get
Return m_BackColor
End Get
Set(ByVal Value As Color)
m_BackColor = Value
End Set
End Property
<Description("Gets or sets a value indicating whether column headers are
visible."), _
Category("Appearance")> _
Public Property ColumnHeadersVisible() As Boolean
Get
Return m_ColumnHeadersVisible
End Get
Set(ByVal Value As Boolean)
m_ColumnHeadersVisible = Value
End Set
End Property
<Description("Gets or sets the foreground color of the grid table."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property ForeColor() As Color
Get
Return m_ForeColor
End Get
Set(ByVal Value As Color)
m_ForeColor = Value
End Set
End Property
<Description("Gets or sets the color of grid lines."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property GridLineColor() As Color
Get
Return m_GridLineColor
End Get
Set(ByVal Value As Color)
m_GridLineColor = Value
End Set
End Property
<Description("Gets or sets the style of grid lines."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property GridLineStyle() As DataGridLineStyle
Get
Return m_GridLineStyle
End Get
Set(ByVal Value As DataGridLineStyle)
m_GridLineStyle = Value
End Set
End Property
<Description("Gets or sets the background color of headers."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property HeaderBackColor() As Color
Get
Return m_HeaderBackColor
End Get
Set(ByVal Value As Color)
m_HeaderBackColor = Value
End Set
End Property
<Description("Gets or sets the font used for header captions."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property HeaderFont() As Font
Get
Return m_HeaderFont
End Get
Set(ByVal Value As Font)
m_HeaderFont = Value
End Set
End Property
<Description("Gets or sets the foreground color of headers."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property HeaderForeColor() As Color
Get
Return m_HeaderForeColor
End Get
Set(ByVal Value As Color)
m_HeaderForeColor = Value
End Set
End Property
<Description("Gets or sets the color of link text."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property LinkColor() As Color
Get
Return m_LinkColor
End Get
Set(ByVal Value As Color)
m_LinkColor = Value
End Set
End Property
<Description("Gets or sets the name used to map this table to a specific
data source."), _
Category("General"), _
DefaultValue(True)> _
Public Property MappingName() As String
Get
Return m_MappingName
End Get
Set(ByVal Value As String)
m_MappingName = Value
End Set
End Property
<Description("Gets or sets the width used to create columns when a new
grid is displayed."), _
Category("General"), _
DefaultValue(True)> _
Public Property PreferredColumnWidth() As Integer
Get
Return m_PreferredColumnWidth
End Get
Set(ByVal Value As Integer)
m_PreferredColumnWidth = Value
End Set
End Property
<Description("Gets or sets the height used to create a row when a new grid
is displayed."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property PreferredRowHeight() As Integer
Get
Return m_PreferredRowHeight
End Get
Set(ByVal Value As Integer)
m_PreferredRowHeight = Value
End Set
End Property
<Description("Gets or sets a value indicating whether columns can be
edited."), _
Category("Usability"), _
DefaultValue(True)> _
Public Property [ReadOnly]() As Boolean
Get
Return m_ReadOnly
End Get
Set(ByVal Value As Boolean)
m_ReadOnly = Value
End Set
End Property
<Description("Gets or sets a value indicating whether row headers are
visible."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property RowHeadersVisible() As Boolean
Get
Return m_RowHeadersVisible
End Get
Set(ByVal Value As Boolean)
m_RowHeadersVisible = Value
End Set
End Property
<Description("Gets or sets the width of row headers."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property RowHeaderWidth() As Integer
Get
Return m_RowHeaderWidth
End Get
Set(ByVal Value As Integer)
m_RowHeaderWidth = Value
End Set
End Property
<Description("Gets or sets the background color of selected cells."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property SelectionBackColor() As Color
Get
Return m_SelectionBackColor
End Get
Set(ByVal Value As Color)
m_SelectionBackColor = Value
End Set
End Property
<Description("Gets or sets the foreground color of selected cells."), _
Category("Appearance"), _
DefaultValue(True)> _
Public Property SelectionForeColor() As Color
Get
Return m_SelectionForeColor
End Get
Set(ByVal Value As Color)
m_SelectionForeColor = Value
End Set
End Property
<Description("Gets or sets the collection of columns available for display
in the datagrid."), _
Category("Data"), Browsable(True), Bindable(True),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content), _
Editor(GetType(ColumnCollectionEditor), _
GetType(UITypeEditor))> _
Public Property Columns() As Columns
Get
Return m_Columns
End Get
Set(ByVal Value As Columns)
m_Columns = Value
End Set
End Property
#End Region
Friend Class DatatableManagerTypeConverter
Inherits ExpandableObjectConverter
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
Return String.Format("Data table manager")
End Function
Public Overloads Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
End Class
End Class
#Region " Column Collection "
<Serializable(), TypeConverter(GetType(Columns.ColumnsTypeConverter))> _
Public Class Columns
Inherits CollectionBase
Event PropertyChaged()
'<DesignerSerializationVisibility(DesignerSerializationVisibility.Content)>
Default Public Property Item(ByVal index As Integer) As BaseColumn
Get
Try
Return CType(List(index), BaseColumn)
Catch ex As Exception
Return Nothing
End Try
End Get
Set(ByVal Value As BaseColumn)
List(index) = Value
End Set
End Property
Public Function Add(ByVal value As BaseColumn) As Integer
Return List.Add(value)
End Function 'Add
Public Sub AddRange(ByVal Items() As BaseColumn)
Dim Item As Object
For Each Item In Items
CType(Item, BaseColumn).Collection = Me
list.Add(Item)
Next
End Sub
Public Function IndexOf(ByVal value As BaseColumn) As Integer
Return List.IndexOf(value)
End Function 'IndexOf
Public Sub Insert(ByVal index As Integer, ByVal value As BaseColumn)
List.Insert(index, value)
End Sub 'Insert
Public Sub Remove(ByVal value As BaseColumn)
List.Remove(value)
End Sub 'Remove
Public Function Contains(ByVal value As BaseColumn) As Boolean
' If value is not of type BaseColumn, this will return false.
Return List.Contains(value)
End Function 'Contains
Protected Shadows Sub OnInsert(ByVal index As Integer, ByVal value As
BaseColumn)
' Insert additional code to be run only when inserting values.
End Sub 'OnInsert
Protected Shadows Sub OnRemove(ByVal index As Integer, ByVal value As
BaseColumn)
' Insert additional code to be run only when removing values.
End Sub 'OnRemove
Protected Shadows Sub OnSet(ByVal index As Integer, ByVal oldValue As
BaseColumn, ByVal newValue As BaseColumn)
' Insert additional code to be run only when setting values.
End Sub 'OnSet
Protected Shadows Sub OnValidate(ByVal value As BaseColumn)
'If Not value.GetType() Is Type.GetType("BaseColumn") Then
'Throw New ArgumentException("value must be of type BaseColumn.",
"value")
'End If
End Sub 'OnValidate
Friend Sub RaisePropertyChangedEvent()
RaiseEvent PropertyChaged()
End Sub
Friend Class ColumnsTypeConverter
Inherits ExpandableObjectConverter
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
Return String.Format("Collection")
End Function
Public Overloads Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
End Class
End Class
#Region " ColumnCollectionEditor"
Friend Class ColumnCollectionEditor
Inherits System.ComponentModel.Design.CollectionEditor
Private Types() As System.Type
Sub New(ByVal type As System.Type)
MyBase.New(type)
Types = New System.Type() {GetType(BaseColumn), GetType(TextColumn),
GetType(CheckColumn)}
End Sub
Protected Overrides Function CreateNewItemTypes() As System.Type()
Return Types
End Function
End Class
#End Region
#Region " Columns Types"
<Serializable(), TypeConverter(GetType(BaseColumn.BaseColumnConverter))> _
Public Class BaseColumn
Public Enum ColumnTypes
Base = 1
Text = 2
Bool = 3
End Enum
#Region " Declarations "
Protected m_Alignment As System.Windows.Forms.HorizontalAlignment
Protected m_HeaderText As String
Protected m_MappingName As String
Protected m_NullText As String
Protected m_ReadOnly As Boolean
Protected m_Width As Integer
Protected m_Visible As Boolean
Protected m_Collection As Columns
Protected m_ColumnType As ColumnTypes
#End Region
#Region " Properties"
<Description("Gets or sets the alignment of text in a column."), _
Category("General"), _
DefaultValue(System.Windows.Forms.HorizontalAlignment.Left)> _
Public Property Alignment() As System.Windows.Forms.HorizontalAlignment
Get
Return m_Alignment
End Get
Set(ByVal Value As System.Windows.Forms.HorizontalAlignment)
m_Alignment = Value
End Set
End Property
<Description("Gets or sets the text of the column header."), _
Category("General"), _
DefaultValue("")> _
Public Property HeaderText() As String
Get
Return m_HeaderText
End Get
Set(ByVal Value As String)
m_HeaderText = Value
End Set
End Property
<Description("Gets or sets the name used to map the column style to a data
member."), _
Category("General"), _
DefaultValue("")> _
Public Property MappingName() As String
Get
Return m_MappingName
End Get
Set(ByVal Value As String)
m_MappingName = Value
End Set
End Property
<Description("Gets or sets the text that is displayed when the column
contains a null reference (Nothing in Visual Basic)."), _
Category("General"), _
DefaultValue("")> _
Public Property NullText() As String
Get
Return m_NullText
End Get
Set(ByVal Value As String)
m_NullText = Value
End Set
End Property
<Description("Gets or sets a value indicating whether the data in the
column can be edited."), _
Category("General"), _
DefaultValue(True)> _
Public Property [ReadOnly]() As Boolean
Get
Return m_ReadOnly
End Get
Set(ByVal Value As Boolean)
m_ReadOnly = Value
End Set
End Property
<Description("Gets or sets the width of the column."), _
Category("General"), _
DefaultValue(50)> _
Public Property Width() As Integer
Get
Return m_Width
End Get
Set(ByVal Value As Integer)
m_Width = Value
End Set
End Property
<Description("Gets or sets weather the column should be displayed in the
datagrid."), _
Category("General"), _
DefaultValue(True)> _
Public Property Visible() As Boolean
Get
Return m_Visible
End Get
Set(ByVal Value As Boolean)
m_Visible = Value
End Set
End Property
Public WriteOnly Property Collection() As Columns
Set(ByVal Value As Columns)
m_Collection = Value
End Set
End Property
<Browsable(False)> Public Property ColumnType() As ColumnTypes
Get
Return m_ColumnType
End Get
Set(ByVal Value As ColumnTypes)
m_ColumnType = Value
End Set
End Property
#End Region
#Region " Methods"
Protected Sub PropertyChanged()
'Check if the collection is a valid object if not during design time you
and up with a message 'Object is not set to an Instance' But Your Program
Works
'By the way I am looking forward for the IsNot operator in VS 2005,
because every time I forget the Not Operator and need to navigate back
If Not m_Collection Is Nothing Then
m_Collection.RaisePropertyChangedEvent()
End If
End Sub
Public Overrides Function ToString() As String
Return m_MappingName
End Function
#End Region
Friend Class BaseColumnConverter
Inherits ExpandableObjectConverter
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
Dim mi As BaseColumn = CType(value, BaseColumn)
Return String.Format("{0}", mi.MappingName)
End Function
Public Overloads Function CanConvertFrom(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal sourceType As
System.Type) As Boolean
If (sourceType Is GetType(String)) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
End Class
End Class
<Serializable(), TypeConverter(GetType(TextColumn.TextColumnConverter))> _
Public Class TextColumn
Inherits BaseColumn
Private m_Format As String
<Description("Gets or sets the character(s) that specify how text is
formatted."), _
Category("General"), _
DefaultValue("")> _
Public Property Format() As String
Get
Return m_Format
End Get
Set(ByVal Value As String)
m_Format = Value
End Set
End Property
Friend Class TextColumnConverter
Inherits BaseColumn.BaseColumnConverter
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
Dim mi As TextColumn = CType(value, TextColumn)
Return String.Format("{0}", mi.MappingName)
End Function
End Class
End Class
<Serializable(), TypeConverter(GetType(CheckColumn.CheckColumnConverter))> _
Public Class CheckColumn
Inherits BaseColumn
Protected m_TrueValue As Object
Protected m_FalseValue As Object
<Description("Gets or sets the actual value used when setting the value of
the column to true."), _
Category("General"), _
DefaultValue(False)> _
Public Property TrueValue() As String
Get
Return CType(m_TrueValue, String)
End Get
Set(ByVal Value As String)
m_TrueValue = Value
End Set
End Property
<Description("Gets or sets the actual value used when setting the value of
the column to false."), _
Category("General"), _
DefaultValue(False)> _
Public Property FalseValue() As String
Get
Return CType(m_FalseValue, String)
End Get
Set(ByVal Value As String)
m_FalseValue = Value
End Set
End Property
Friend Class CheckColumnConverter
Inherits BaseColumn.BaseColumnConverter
Public Overloads Overrides Function ConvertTo(ByVal context As
System.ComponentModel.ITypeDescriptorContext, ByVal culture As
System.Globalization.CultureInfo, ByVal value As Object, ByVal
destinationType As System.Type) As Object
Dim mi As CheckColumn = CType(value, CheckColumn)
Return String.Format("{0}", mi.MappingName)
End Function
End Class
End Class
#End Region
#End Region