Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding property to datagridview column 1

Status
Not open for further replies.

svagelis

Programmer
May 2, 2001
121
GR
Hello to everyone,
i have created one instance of datagridview on my form(by using the designer), with some columns from a custom class (CustomDataGridViewTextBoxColumn) which inherits from the base DataGridViewTextBoxColumn class.

So far i ve added one extra property on my CustomDataGridViewTextBoxColumn.
It works ok so far and i can set my etxra property from within the designer.

The problem is that i cannot reference to this property from within my form by using datagridview1.columns("Test").CustomProperty.

This is a part of code that the designer has generated or forms load event

--------------------------------------------------------

Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.Column1 = New WindowsApplication1.CustomDataGridViewTextBoxColum n

Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1})

Me.Column1.HeaderText = "Column1"
Me.Column1.myProperty = "1"
Me.Column1.Name = "Column1"

Me.Controls.Add(Me.DataGridView1)
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)

--------------------------------------------------------------------



I can access my property by typing me.myproperty, but by using

Me.DataGridView1.Columns("test").myproperty i get error.

Well, i guess what when adding the custom columns to the datagrid, whose columns are converted to type DataGridViewColumn(which doesnt have my property).
Is it possible to add this property to the DataGridViewColumn class because its something i want to to for all the columns.

Thanks in advance!




below is my code
-----------------------


Option Explicit On
Imports System.ComponentModel

Public Class CustomDataGridViewTextBoxColumn
Inherits System.Windows.Forms.DataGridViewTextBoxColumn

Private mProperty As String

Public Property myProperty() As String
Get
Return mProperty
End Get
Set(ByVal value As String)
mProperty = value
End Set
End Property

Public Overrides Function Clone() As Object
Dim Column As CustomDataGridViewTextBoxColumn = CType(MyBase.Clone(),

CustomDataGridViewTextBoxColumn)
Column.myProperty = mProperty
Return Column
End Function

Public Sub New()
MyBase.New()
End Sub

End Class





<Global.Microsoft.VisualBasic.CompilerServices.Des ignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
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.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.DataGridView1 = New System.Windows.Forms.DataGridView
Me.Column1 = New WindowsApplication1.CustomDataGridViewTextBoxColum n
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginIni t()
Me.SuspendLayout()
'
'DataGridView1
'
Me.DataGridView1.ColumnHeadersHeightSizeMode =

System.Windows.Forms.DataGridViewColumnHeadersHeig htSizeMode.AutoSize
Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn()

{Me.Column1})
Me.DataGridView1.Location = New System.Drawing.Point(25, 58)
Me.DataGridView1.Name = "DataGridView1"
Me.DataGridView1.Size = New System.Drawing.Size(378, 89)
Me.DataGridView1.TabIndex = 0
'
'Column1
'
Me.Column1.HeaderText = "Column1"
Me.Column1.myProperty = "1"
Me.Column1.Name = "Column1"
Me.Column1.Resizable = System.Windows.Forms.DataGridViewTriState.[True]
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(450, 293)
Me.Controls.Add(Me.DataGridView1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit( )
Me.ResumeLayout(False)

End Sub
Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView
Friend WithEvents Column1 As WindowsApplication1.CustomDataGridViewTextBoxColumn
End Class
 
First I would really ask what are you wanting it for? What your attempting to do may not net you the actual results you want. That said "datagridview1.columns("Test").CustomProperty" doesn't work because it isn't a property of datagridview1.columns, but a property of a specific column type. datagridview1.columns is a group of types that may or may not include your type. So this code you posted should be correct:

Code:
Me.DataGridView1 = New System.Windows.Forms.DataGridView
[red]Me.Column1 = New WindowsApplication1.CustomDataGridViewTextBoxColumn[/red]

Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1})

Me.Column1.HeaderText = "Column1"
[red]Me.Column1.myProperty = "1"[/red]
Me.Column1.Name = "Column1"

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I should have said it is a group of objects not types.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
oops I messed up too I forgot to add the part about getting it. It has to be done in the same manner.

Code:
        Dim CustomColumn As CustomDataGridViewTextBoxColumn
        CustomColumn = Me.DataGridView1.Columns("Column1")
        CustomColumn.myProperty = "something here"

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for the reply, i knew the problem but i didnt think that was so simple.

-- First I would really ask what are you wanting it for?

I want to add some properties to column types in order to add robust functionality (from code inside column class, and code inside form class)

As soon as i get it to work, i ll post it...

Well for now, one star for you...
 
np. Thank you. Sure I like to see it. I've not had anything come up where I would want an additional property that would apply to the whole column. Still that is the power of programming, in general, that we can make the things we come up with true. :)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top