Results 1 to 5 of 5

Thread: problem in work with datagridviewcolumns class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    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?

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    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?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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:
    1. Public Class MyDatagridviewColumn
    2.     Inherits DataGridViewTextBoxColumn ' NB. the change here DataGridViewColumn is a generic class which has no cell template
    3.     Dim MName As String
    4.     Public Property MyNewProperty As String
    5.         Get
    6.             Return MName
    7.         End Get
    8.         Set(ByVal value As String)
    9.             MName = value
    10.         End Set
    11.     End Property
    12. End Class

    ... you simply add it to the datagridview in the normal way.

    vb.net Code:
    1. Dim mdgCol As New MyDatagridviewColumn
    2.         mdgCol.MyNewProperty = "My New Column Property"
    3.         mdgCol.HeaderText = mdgCol.MyNewProperty
    4.         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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    72

    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
  •  



Click Here to Expand Forum to Full Width