troubleshooting Question

Create a new DATAGRIDVIEWCELL property

Avatar of lcmcmillin
lcmcmillin asked on
Visual Basic.NET
8 Comments1 Solution1687 ViewsLast Modified:
I've tried researching this but unfortunately I cannot find or figure out the answer.  Is it possible?  
I would like to add an additional property at the CurrentCell level, in this example named Tag2.

With the code below the following immediate window commands yield the displayed results:
'NewProperty' is not a member of 'DataGridViewColumn'.
'Tag2' is not a member of 'DataGridViewCell'.

So two questions really:
  1)  How to add "Tag2" to each cell in a DataGridView control?
  2)  Why is NewProperty only available to "col" and not to dg1.column(0)?


Public Class Form1
  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim dg1 As New DataGridView
    Dim col As New DataGridViewMyColumn
    col.CellTemplate = New DataGridViewMyCell
  End Sub
End Class
Public Class DataGridViewMyCell
  Inherits DataGridViewCell
  Private m_Tag2 As String
  Public Overridable Property Tag2() As String
      Return Me.m_Tag2
    End Get
    Set(ByVal value As String)
      Me.m_Tag2 = value
    End Set
  End Property
  Public Sub New()
    Me.m_Tag2 = ""
  End Sub
  Public Overrides Function Clone() As Object
    Dim Cell As DataGridViewMyCell = CType(MyBase.Clone(), DataGridViewMyCell)
    Return Cell
  End Function
End Class
Public Class DataGridViewMyColumn
  Inherits DataGridViewColumn
  Public NewProperty As String
  Public Sub New()
    MyBase.New(New DataGridViewMyCell)
    Me.CellTemplate = New DataGridViewMyCell()
    Me.NewProperty = ""
  End Sub
  Public Overrides Property CellTemplate() As System.Windows.Forms.DataGridViewCell
      Return MyBase.CellTemplate
    End Get
    Set(ByVal value As System.Windows.Forms.DataGridViewCell)
      'Only cell types that derive from MaskedTextBoxCell are supported as the cell template.
      If Not IsNothing(value) And Not value.GetType().IsAssignableFrom(GetType(DataGridViewMyCell)) Then
        Throw New InvalidCastException("Cell type is not based upon the MaskedTextBoxCell")
        MyBase.CellTemplate = value
      End If
    End Set
  End Property
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 8 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros