Question question about add a property under columns() property in Datagridview

pooya1072

Well-known member
Joined
Jul 5, 2012
Messages
84
Programming Experience
Beginner
hi
yesterday i asked a question related by creating new class and inheritance.
some friends answer . but in an exercise i have a problem. in Datagridview there is a property named Columns . it can used in 3 way .
1-without parameter
2-with index
3-with column name

i want to add a new propery named Equation under columns(index) . so after that i must could do this...
for example i want in column by index=1 do this :
VB.NET:
[FONT=Courier New]Datagridview1.columns(1).Equation=""[/FONT]




what is the steps of this?
 
You're still misunderstanding what's going on there. Firstly, the DataGridView.Columns property can only be used one way. It's a ReadOnly property and it returns a DataGridViewColumnCollection and that's it. What you then do with that collection is up to you. It is just like any other collection.

When you index it by ordinal as you have in your example, what you're actually doing is invoking the overloaded Item property and specifying the index of the item you want to get, i.e.
DataGridView1.Columns.Item(1)
That Item property has a second overload that will accept a column name. Both overloads of that property return a DataGridViewColumn.

Now, you can't just magically add properties to existing classes. Whoever defined the class in the first place defines what properties it has and that's that. If you want to add a property then you have to ad it to your own class. One option would be to inherit all the column types that you want to use and add such a property, e.g.
Public Class DataGridViewTextBoxColumnEx
    Inherits DataGridViewTextBoxColumn

    Public Property Equation() As String

End Class
You can now add DataGridViewTextBoxColumnEx objects to the Columns collection of your DataGridView instead of the standard DataGridViewTextBoxColumns. You can then do this in code:
DirectCast(DataGridView1.Columns(1), DataGridViewTextBoxColumnEx).Equation = ""
Another option might be to use extension methods. Extension methods allow you to call them in code as though they are instance methods of an object when, in reality, they are external methods that receive that object as an argument. That means that they are limited to accessing only public members of the object, where instance members can access private members too.

As an example, you could store the columns and their corresponding equations in a Dictionary, e.g.
Public Module DataGridViewColumnExtensions

    Private ReadOnly equationsByColumn As New Dictionary(Of DataGridViewColumn, String)

    Public Function GetEquation(column As DataGridViewColumn) As String
        Return equationsByColumn(column)
    End Function

    Public Sub SetEquation(column As DataGridViewColumn, equation As String)
        equationsByColumn(column) = equation
    End Sub

End Module
You can then do this in code:
SetEquation(DataGridView1.Columns(1), "")
You can change the module to make those methods extensions:
Imports System.Runtime.CompilerServices

Public Module DataGridViewColumnExtensions

    Private ReadOnly equationsByColumn As New Dictionary(Of DataGridViewColumn, String)

    <Extension>
    Public Function GetEquation(column As DataGridViewColumn) As String
        Return equationsByColumn(column)
    End Function

    <Extension>
    Public Sub SetEquation(column As DataGridViewColumn, equation As String)
        equationsByColumn(column) = equation
    End Sub

End Module
and then call them in code as though they were instance methods, e.g.
DataGridView1.Columns(1).SetEquation("")
 

Latest posts

Back
Top