Results 1 to 19 of 19

Thread: Add Properties To DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Add Properties To DataGridView

    Could someone show me how to create 2 new properties for the DataGridView?

    1. Add a .Info property to each Rolumn of the DataGridView.

    2. Add a .LastName property to each Row of the DataGridView.


    So when the Grid's CellChanged even fires, I can do something like this...
    TextBox1.Text = DataGridView1.Columns(theCurrentColumn).Info

    Is that possible?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Add Properties To DataGridView

    You'd have to create two classes that derives from the DataGridViewRow and DataGridViewColumn classes. Properties, and their variables, are created like this:
    vb Code:
    1. Private _myVariable As MyType
    2.     Public Property ThisIsMyProperty() As MyType
    3.         Get
    4.             Return _myVariable
    5.         End Get
    6.         Set(ByVal value As MyType)
    7.             _myVariable = value
    8.         End Set
    9.     End Property
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    I understand the workings of Get and Set as they apply to properties of a new Class. What I don't know, is how I'm going to apply this information to creating a new UserControl that looks and acts just like a DataGridView but with my new properties.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    Will I actually have to create a UserControl or will I simply be able to edit the DataGridView class and use Inherits?

    I'm confused.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    Actually, I need to be able to add a couple of properties to each cell in the grid too. I gave it a try at home but I didn't get very far.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    In trying to get a new property for each cell and for each column, I've tried the following...

    Code:
    Public Class myDataGridViewTextBoxCell
        Inherits DataGridViewTextBoxCell
    
        Public GradeValue As String
        Public Info As String
    
        Public Function PercentCorrect() As Integer
            Return 100
        End Function
    
    End Class
    and

    Code:
    Public Class myDataGridViewColumn
        Inherits DataGridViewColumn
    
        Public AssignmentPage As String = ""
        Public DateAssigned As Date
        Public DateTurnedIn As Date
        Public PerfectScore As Integer = 100
    
        Public Sub New()
            Me.CellTemplate = New myDataGridViewTextBoxCell()
            Me.ReadOnly = False
        End Sub
    End Class
    I'm expecting my grid to now be able to create a column of type myDataGridViewColumn which will have the extra properties. But I still only have access to the standard DataGridView columns that are normally available. How do I get to this new column type that I've created?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    I got the custom column type to show up now by doing a Build from the menus. But I still don't have access to my new properties.

    I tried...

    DataGridView1.Columns(1).PerfectScore = 100

    But PerfectScore is not a member....

    Now what?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Properties To DataGridView

    Quote Originally Posted by DroopyPawn
    I got the custom column type to show up now by doing a Build from the menus. But I still don't have access to my new properties.

    I tried...

    DataGridView1.Columns(1).PerfectScore = 100

    But PerfectScore is not a member....

    Now what?
    Columns is a collection of DataGridViewColumn objects. It has to be able to store all sorts of column types, so each column gets returned as the lowest common denominator: DataGridViewColumn. You have to cast that as your specific type in order to access your specific properties. It's exactly the same as an ArrayList returning each item as an Object reference and your having to cast those items as their specific type to access their specific properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    Yep that did it. Thanks.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    Now I'm a little confused...

    In the code below, I add my new column to my DataGridView (named grd) and give the column's AssignmentDescription a value of "1st value"

    The form has 2 buttons. Button one verifies that "1st value" is actually in the column's AssignmentDescription with a messagebox. Then I set the AssignmentDescription to "2nd value" but it's done in the variable c.

    I know that c is a myDataGridViewColumn with my properties and is equal to the column that I actually added to the grid. But in this case, it seems that c acually "IS" the column that I added. Is that right?



    Also, I want to add a few custom properties to each row of the DataGridView. I've already created a class called myDataGridViewRow and added my specific new properties. How can I make sure those properties are available to the rest of my program? I assume it will have something to do with grd.Rows.Add or something like that. Am I far off?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    ....

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim col As New myDataGridViewColumn
    5.         grd.Columns.Add(col)
    6.         col.AssignmentDescription = "1st value"
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         Dim v As String
    11.         Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn)
    12.         v = c.AssignmentDescription
    13.         MsgBox(v)
    14.         c.AssignmentDescription = "2nd value"
    15.     End Sub
    16.  
    17.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    18.         Dim v As String
    19.         Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn)
    20.         v = c.AssignmentDescription
    21.         MsgBox(v)
    22.     End Sub
    23.  
    24. End Class

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Properties To DataGridView

    Quote Originally Posted by DroopyPawn
    I know that c is a myDataGridViewColumn with my properties and is equal to the column that I actually added to the grid. But in this case, it seems that c acually "IS" the column that I added. Is that right?
    That's how reference types work. Every class is a reference type and DataGridViewColumn is a class. Your type is a class too.

    Reference type variables do not contain objects. They contain a reference to an object. A reference is basically a tarted-up pointer, i.e. a memory address. So, when you create your column it is stored in an area of memory known as the heap. Variables are stored on the stack. Any variable that refers to your column contains the address at which it is stored at on the heap.

    If you assign one variable to another you copy the content of one variable to another. If the original variable contain the memory address of an object then the new variable now contains that same memory address, therefore both variables refer to the same object.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Properties To DataGridView

    If you want to create a row in a DataGridView then you would need to inherit the DataGridViewRow class, but you're limited in how you can use it. In many cases the grid itself creates the rows and, unless you inherit and modify the DataGridView class too, no grid is going to create your custom rows. That means that you'd have to create instances of your class externally and then add them to the grid explicitly. This precludes you from using data-binding, which would generate standard rows.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    My grid is unbound. I had already tried adding my custom row to the DataGridView (grd) but I didn't have any luck.

    So if I create another class that inherits from DataGridView, what would I have to do to it to enable my custom rows, which I would add programatically?

    And would I have to do anything extra to make it use my custom columns or would that part still be the same as it is now?

    Thanks for the help.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Properties To DataGridView

    I already said. Create instances of your row class and add them to the grid. You're already doing it for your column class. It's no different for the rows, except you add to the Rows collection instead of the Columns.
    Last edited by jmcilhinney; Mar 23rd, 2008 at 07:44 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    I've tried the code bellow. The column properties work as expected but the Button1_Click sub returns nothing in the 2nd message box.
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim col As New myDataGridViewColumn
    5.         grd.Columns.Add(col)
    6.         col.AssignmentDescription = "1st value"
    7.  
    8.         Dim r As New myDataGridViewRow
    9.         grd.Rows.Add(r)
    10.         r.Name_First = "Joe1"
    11.  
    12.     End Sub
    13.  
    14.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    15.         Dim v As String
    16.         Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn)
    17.         v = c.AssignmentDescription
    18.         MsgBox(v)
    19.         c.AssignmentDescription = "2nd value"
    20.  
    21.         Dim r As myDataGridViewRow = CType(grd.Rows(0), myDataGridViewRow)
    22.         MsgBox(r.Name_First)
    23.  
    24.     End Sub
    25.  
    26.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    27.         Dim v As String
    28.         Dim c As myDataGridViewColumn = CType(grd.Columns(0), myDataGridViewColumn)
    29.         v = c.AssignmentDescription
    30.         MsgBox(v)
    31.     End Sub
    32.  
    33. End Class

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Add Properties To DataGridView

    And what exactly does the implementation of your Name_First property look like? Have you debugged it to make sure that the data is going where you think it is?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: Add Properties To DataGridView

    I got it working. Thanks.

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