|
-
May 3rd, 2013, 11:09 AM
#1
Thread Starter
Lively Member
problem in work with datagridviewcolumns class
hi friends
i create a new class that inherits datagridview . my main purpose is to add a new property to all columns of this new datagridview .
my professor want that i do this in the form below . i explain to you all the steps and my problem :
1- i create a new class of DatagridviewColumns :
Code:
Public Class MyDatagridviewColumn
Inherits DataGridViewColumn
Dim MName As String
Public Property MyNewProperty As String
Get
Return MName
End Get
Set(ByVal value As String)
MName = value
End Set
End Property
End Class
2- i create a new class inherits of datagridview :
Code:
Public Class MyDatagriedview
Inherits DataGridView
Dim C1 As MyDatagridviewColumn
Dim CC1 As DataGridViewColumnCollection
Public Shadows Property Columns(ByVal Index As Integer) As MyDatagridviewColumn
Get
Return C1
End Get
Set(ByVal value As MyDatagridviewColumn)
C1 = value
End Set
End Property
Public Shadows Property Columns As DataGridViewColumnCollection
Get
Return CC1
End Get
Set(ByVal value As DataGridViewColumnCollection)
CC1 = value
End Set
End Property
Public Sub Initialize()
C1 = New MyDatagridviewColumn
CC1 = New DataGridViewColumnCollection(Me)
End Sub
Sub New()
Initialize()
End Sub
End Class
3- now i add it to my form in runtime and add on example column to it :
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mdgv As New MyDatagriedview
mdgv.Columns.Add("c1", "c2")
Me.Controls.Add(mdgv)
End Sub
End Class
but it dont work . form is load and a datagridview with no column is appear . what and where is the problem?
-
May 3rd, 2013, 11:31 AM
#2
Re: problem in work with datagridviewcolumns class
I hardly know where to begin. I'm not sure why no column is shown but it's obvious why it's not one of your custom columns. Your MyDatagriedview class (and you couldn't spare the time to correct the typo?) doesn't actually do anything new at all. If you want .Columns.Add() to add a different kind of column (I'll leave aside my doubts about what you gain from this alleged customisation that you don't get from the columns header or tag properties!) then you need to override or overload that method. Shadowing the two properties as you have does nothing useful at all and probably explains why the control comes up empty.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 3rd, 2013, 11:47 AM
#3
Thread Starter
Lively Member
Re: problem in work with datagridviewcolumns class
sure .. now i explain all i need to do and please you show me the way ...
i have a datagridview that it bind to a sql database via a Datatable . all rows and columns add to datagridview with DataAdapter.Fill command .
so i want all columns like Columns(0) or Columns(1) or .... has a New Property For example Named MyNewProperty .
how can i do this ? is it possible?
-
May 3rd, 2013, 12:25 PM
#4
Re: problem in work with datagridviewcolumns class
You're really overthinking this. All you are doing is adding a new column style to those that are available to the DataGridView control. So once you have created you column class ....
vb.net Code:
Public Class MyDatagridviewColumn
Inherits DataGridViewTextBoxColumn ' NB. the change here DataGridViewColumn is a generic class which has no cell template
Dim MName As String
Public Property MyNewProperty As String
Get
Return MName
End Get
Set(ByVal value As String)
MName = value
End Set
End Property
End Class
... you simply add it to the datagridview in the normal way.
vb.net Code:
Dim mdgCol As New MyDatagridviewColumn
mdgCol.MyNewProperty = "My New Column Property"
mdgCol.HeaderText = mdgCol.MyNewProperty
DataGridView1.Columns.Add(mdgCol)
There really is no need to change the DGV control itself in any way.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
May 3rd, 2013, 01:32 PM
#5
Thread Starter
Lively Member
Re: problem in work with datagridviewcolumns class
thanks ... i will try to use your guide
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|