Problem with class inherited from DataGridView (VS-2005)

U

uncle.serge

In my app's form there are 8 instances of the same (functionally same)
grid which should handle data of URL type (same 3 columns, same
validations, same data classes, same visual behavior). Instead of
recreating in VS Designer this grid 8 times, I wrote a class UrlGrid
which inherits from standard DataGridView. In constructor of UrlGrid I
added 3 columns to it and set all properties. All is good for now and
the new control even automatically appears on Designer toolbox. But
when I drop this grid on a form and save it, all the columns are
doubled! I figured out why it's happening and fixed this but adding
the following in UrlGrid:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
private new DataGridViewColumnCollection Columns { get { return
base.Columns; } }

The DesignerSerializationVisibility attibutes tells Designer not to
save Coulmns property in MyForm.Designer.cs file. It helped a bit: in
terms of getting rid of doubling the coulmns. But still the columns
still are created in the form as form's variables. They are not added
to the grid instance but keep being generated on every change-and-save
of MyForm.cs[Design], resulting in extra 24 (8*3) "hanging" objects of
type DataGridViewColumn and about of 300 lines of code where their
numerous properties are set. By "hanging" I mean that these objects
are not used anywhere. But still, after 2 weeks of working on the
form, its designer-file has become more than 120,000 lines of code and
about 1MB in size, dramatically increasing total time of edit-save-
compile-run cycle.

So, the question: how to tell Designer not to generate Columns
variables for this custom grid class? I tried [Browsable(false)] and
some other attribute but it didn't help.
 
U

uncle.serge

For those who have or will have similar problems: I fixed this only by
creating a new User Control (a class based on UserControl class), not
Inherited User Comtrol as one might think and as would be quite
logical. In fact, it's impossible to create an Inherited User Control
based on DataGridView or any other standard .NET control (which was
previously known as window subclassing - a wide-spread practice). Why?
Ask MS guys.

In Designer, I put DataGridView on my user control, set grid's Dock to
Fill, added three columns and event handlers for the grid. Then, I
deleted my old grids from the form and added this new control in 8
places. Only this way, by adding an extra container in form of
UserControl class, it's possible make VS Designer happy and be able to
reuse a customized version of DataGrid with its own collection of
Columns.

NB. New user control will automatically appear in Designer Toolbox (in
Custom Controls section) once you successfully compile the project.
 

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