Results 1 to 7 of 7

Thread: DataGridView Custom Properties

  1. #1

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

    DataGridView Custom Properties

    I'd like to add some custom properties to the columns of my DataGridView control. I'm not sure how to do it.

    I tried this once before but never got it finished. Basically, I'm making a teacher gradebook. Fore each column, I need to add properties called Assignment, DateAssigned, DateDue, PerfectScore, AssignmentType, etc.

    Any ideas?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: DataGridView Custom Properties

    If you're describing what I think you are then what you want is called Inheritance. There's a lot of inheritance tutorials online if you google for them.

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

    Re: DataGridView Custom Properties

    Those really don't sound like properties that should be added to a DataGridViewColumn because they have nothing to do with displaying data in a DataGridView. They sound like they ARE the data. Can you explain exactly what you are trying to achieve because I'm fairly sure that there's a better way than adding those properties to a DataGridViewColumn.
    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

  4. #4

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

    Re: DataGridView Custom Properties

    I want to keep the data for each assignment (each column in the DataGridView) with the actual student grades. I don't want to bother with arrays.

    I know that I need to create a custom control that inherits the DataGridView but the DataGridView itself is a collection of cell collected into columns, etc.

    The whole point is to keep the data in the control, not in arrays. I suppose I could add a few extra hidden rows to the grid and store the data there (and I might do that). But then if I add a new row (a new student) I think it would get added as the last row - then I'd have to move all my info in the hidden rows down one row.

    Know what I mean Vern?

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

    Re: DataGridView Custom Properties

    As far as what you're actually asking for, you don't need to change the DataGridView control at all. You simply need to inherit the DataGridViewTextBoxColumn (or some other column type if it's more appropriate) and then declare your properties as you would for any other class. You then add instances of your column type to the grid instead of the usual text box columns.

    I still think that this is a bad idea though. You should be defining your own Assignment class and add these properties to that. You can then setup a correspondence between a column and an Assignment using a Dictionary if you like. You say that you don't want to "bother" with arrays but you should do what is proper and if the use of one or more arrays is appropriate then that's what you should be doing.
    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

  6. #6

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

    Re: DataGridView Custom Properties

    Why would I want to do an Assignment Class and not and Assignment Structure? What's the advantage?

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

    Re: DataGridView Custom Properties

    A structure may well be fine. If the type is small and simple then a structure will do just as well or maybe even better. The general rule is that if an object occupies more than 16 bytes then its type should be a class. Note that this means the object itself, not objects that it might refer to. If an object has a String field then that field occupies 4 bytes, no matter how long the string is. The object contains a reference to the string only.
    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

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