DataGridView inheritance

N

Nick Locke

I have just found that there is a feature in VS .NET which deliberately
stops me making a DGV "protected" in an ancestor class and then modifying
its properties in inherited classes. I can see why this problematic for MS
and therefore why it has not been implemented. However, I could do with a
suggestion on how best to move forwards with what I need to do.....

I have an ancestor UserControl which contains a single DGV and lots of
methods/properties to allow me to "talk" with that DGV from a containing
form.

I then need around 30 UserControls inherited from that ancestor. They all
need to offer the same methods/properties, but will need different data
connections from their instance of the DGV. They will also need different
columns displayed, with different headings and so on.

I could obviously bin the OO approach altogether and just copy my control 30
times - but I am fearful of then needing to make every change 30 times in
the future.

Suggestions please!

Thanks, Nick.
 
N

Nick Locke

Thinking of one way for myself (maybe). Would it make sense to "paint" each
DGV separately (eg on a form)and then paste the code from the designer into
my inherited class somewhere?
 
K

Kevin Spencer

You don't have to inherit a DataGridView to create a new class. You can
create a container class that implements and exposes a DataGridView.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
N

Nick Locke

Kevin said:
You don't have to inherit a DataGridView to create a new class. You can
create a container class that implements and exposes a DataGridView.

Kevin, if I am understanding correctly, you are saying that I don't need to
create a User Control, put a DGV on it and then use that as an ancestor
class for all of my objects. Rather, I think you are suggesting that I
create a customised DGV (encapsulating all of my functionality) and then I
just plonk one of those onto each of my target user controls and/or forms.
Am I on the right wavelength?

So, to "create a container class that implements and exposes a
DataGridView", I was hoping that when I did this:

Public Class CustomDGV
Implements System.Windows.Forms.?????

End Class

I would just be able to implement a DGV like that. I am showing my
ignorance here, but need a bit more of a pointer.

Thanks,

Nick
 
N

Nick Locke

Have I answered my own question - is it just:

Public Class CustomDGV

Inherits System.Windows.Forms.DataGridView

End Class

Thanks?!
 
K

Kevin Spencer

That is one solution. The other is to create a class that implements and
exposes a DataGridView, such as the following (forgive my C#):

public class DataGridViewContainer : UserControl
{
private DataGridView _View;

public int ViewWidth
{
get { return _View.Width; }
set { _View.Width = value; }
}

public DataGridView View { get { return _View; } }
// etc.
}

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
N

Nick Locke

Nick said:
Public Class CustomDGV
Inherits System.Windows.Forms.DataGridView

overriden and/or additional functions, methods, events etc

End Class


Kevin said:
That is one solution. The other is to create a class that implements and
exposes a DataGridView, such as the following (forgive my C#):

public class DataGridViewContainer : UserControl
{
private DataGridView _View;

public int ViewWidth
{
get { return _View.Width; }
set { _View.Width = value; }
}

public DataGridView View { get { return _View; } }
// etc.
}

Thanks Kevin, that certainly does help. Any views (from Kevin or anyone),
whether there are reasons to choose one approach over the other (hidden
gotchas, etc)?

Thanks
 
K

Kevin Spencer

When you implement a DataGridView rather than inheriting it, you have more
control over what is exposed and what is not. That's about the only
advantage I can think of.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top