Results 1 to 5 of 5

Thread: Changing an existing component

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2009
    Location
    Niagara Falls
    Posts
    14

    Changing an existing component

    Hello,

    I have a large project which consists of about 60 modules. It contains a lot of DataGridView components.

    Recently I implemented cut-copy-paste functionality to one of the DataGridViews.

    What is the best way to add same functionalities to all the other DataGridViews .

    P.S. I don't want to create a new component which inherits DataGridView and re-define all the DataGridViews.

    Thanks
    Erden

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

    Re: Changing an existing component

    Quote Originally Posted by Erden View Post
    I don't want to create a new component which inherits DataGridView and re-define all the DataGridViews.
    That's exactly what you should do. Presumably you have just handled various events and referred directly to your one specific DataGridView in the code you've written. There's no way to reuse that code other than perhaps to copy and paste it multiple times and then fix any errors that generates.

    You're using an OO language so you should be using good OOP practices. The correct way to go about this is to create your own class that does the work internally as much as possible and then use that class in multiple places. Doing so will not require you to delete all your existing DataGridViews from your forms and then add new ones. All you would need to do is go into the designer code file for each form that contains a DataGridView and change this:
    vb.net Code:
    1. Private DataGridView1 As System.Windows.Forms.DataGridView
    2.  
    3. Me.DataGridView1 = New System.Windows.Forms.DataGridView
    to this:
    vb.net Code:
    1. Private DataGridView1 As MyNamespace.MyDerivedDataGridView
    2.  
    3. Me.DataGridView1 = New MyNamespace.MyDerivedDataGridView
    You can then open your forms in the designer and you'll have access to any new properties of your type and you can also handle any new events of your type in code, etc.
    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

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

    Re: Changing an existing component

    The one other option would be to add extension methods to the DataGridView class, which is possible if you're using .NET 3.5. This may not provide all the functionality you need though.
    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
    New Member
    Join Date
    Jun 2009
    Location
    Niagara Falls
    Posts
    14

    Re: Changing an existing component

    Quote Originally Posted by jmcilhinney View Post
    The one other option would be to add extension methods to the DataGridView class, which is possible if you're using .NET 3.5. This may not provide all the functionality you need though.
    Thanks a lot! That is What I was looking for. Yes I am using .NET 3.5. But what do you mean by "This may not provide all the functionality you need though." ?

    Cheers

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

    Re: Changing an existing component

    Quote Originally Posted by Erden View Post
    But what do you mean by "This may not provide all the functionality you need though." ?
    Extension methods can only do so much. They are only methods, for a start. You can't add instance properties or events using extension methods. Also, while they are called as though they are members, they are implemented outside the class, not inside. As such, you have no access to protected members of the class.
    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