DataGridView as Custom Control with a default column: Code executes twice...

DataGridView as Custom Control with a default column: Code executes twice...

Post by Claes Wedi » Wed, 08 Aug 2007 19:22:08


I'm doing a Component class that inherits from DataGridView.
One thing that it does is to add a default column in the grid.
I put the code for this in the class and when I drag the control to my form
i VS2005 it appears just as expected in the designer.
The problem is that when I run the application it adds the column twice so I
get 2 columns in runtime.
The reason is that when I drag my custom control to the form it generates
code i the designer file to add a column.
Then in runtime it runs the code in the forms designer file AND the code in
my Custom class so I get 2 columns...

The problem as I see it is that the drag/drop in VS2005 creates the stuff I
want, but I do not want the code in my class to be run in runtime.

What do I do wrong?

TIA / Claes
 
 
 

DataGridView as Custom Control with a default column: Code executes twice...

Post by Peter Duni » Thu, 09 Aug 2007 02:24:16


Are you sure that's the problem? I mean, it sounds logical, but in fact
the Designer doesn't normally take code-generated stuff and throw it
into the InitializeComponent() method. At least, that hasn't been my
experience.

I don't know for sure what the problem is, but you should definitely
look at the generated InitializeComponent() method for the form
containing your control to see if it really has the same code to add the
column that you've added explicitly in the constructor.

If the Designer is in fact getting confused at copying code-generated
data into the InitializeComponent() method for the control (or for the
containing control, for that matter), then you might be able to work
around the issue by using one of the code attributes defined for working
with the Designer (to disable your column-adding code when your control
is instantiated by the Designer).

Pete

 
 
 

DataGridView as Custom Control with a default column: Code executes twice...

Post by Claes Wedi » Thu, 09 Aug 2007 15:08:19

Hi Pete,
Definitly code is created in the designer file when draging the component
from my toolbox to the vindow:
I get all the properties from my components constructor put into the forms
designer file.

I thought I found one workaround is:
1. Drag a standard DataGridView to the form (puts a few standard properties
in the designer file)
2. Change the taype of the control in the forms designer file:
from: this.dataGridView1 = new System.Windows.Forms.DataGridView();
to: this.dataGridView1 = new MyDataGridView();
(and private DataGridView dataGridView1 ;
to private MyDataGridView dataGridView1;)

This seemed to work the way one wants it to:
It looks OK both i n the Form Designer window AND in runtime...

But if you do anything with the control in the form (change the size) it
copies all properties from from the component into the forms designer file
and you are back where you started...

//Claes