DataGrid & Visual Studio.NET 2003

Hello,
I have derived my own datagrid component from the one readily available in visual studio .net 2003 (using .NET framework 1.1). When I include the component in my project, I have noticed the following annoying behaviour:
(*) Visual Studio .NET generates code for some additional DataGridTableStyles. To be honest, I think I've erased the code for hundreds of these, and they still keep appearing. It seems every time I make a modification to my DataGrid by using a designer (but not only in that case, there are others that I couldn't notice), VS generates code for a new DataGridTableStyle and adds it to my table. This does "wonders" to my code and keeps it from being readable. Also, if I have, say 4 instances of my derived component, then it generates 4 DataGridTableStyles at once. I know that some DataGridTableStyle settins override the original ones in the DataGrid, but still I do not get why VS behaves like this.
(*) Occasionally, the automatically generated code for my component's instances DISAPPEARS. (just like tonight, before me posting this)
I need someone's second opinion on this, because developing this project is becoming a little nightmare just because of this.
[1218 byte] By [AndreiIsmail] at [2007-12-16]
# 1
Are you adding a DataGridTableStyle in your derived DataGrid (in the constructor or some properties) ?
MikeDanes at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
Yes, I am. Does that have to do anything with it? If yes, why? I'm doing it in order to keep some common properties the same for each datagrid i'm using.
AndreiIsmail at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
Yes, it does. If you add a DataGridTableStyle to the TableStyles collection (in the constructor for example) then the designer serializer finds the table style you've just added in the TableStyle collection and writes it.

When you open the form in the designer again, your DataGrid will be instantiated and a new table style is created and added to the TableStyles collection. This one gets written too by the designer and now you have 2 table styles...

Unfortunately I don't have a good workaround right now. I have the same problem and I'll keep diggin'. If I have something I'll let you know.

You could try using the DesignMode property and if it is true then do not add the table style but I don't know if this won't affect the design time functionality of your data grid.

I don't know why your data grid disappears some times. It happend to me at one moment but not anymore. Maybe your code generates some exceptions ?

Note:
Adding an object to a collection that is serialized by the designer is not a DataGrid problem. You could derive from ListBox, add an item in the constructor and you'll have the same problem. The designer has now way of knowing that the object you have added should not be persisted in InitializeComponent code.

MikeDanes at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4
I have something altough is kind of weird. It only works if you use only the table style created in your derived DataGrid. If you add another styles in the designer this will not work.

First add the following to your derived DataGrid:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new WinForms.GridTableStylesCollection TableStyles
{
get
{
return base.TableStyles;
}
}

The DesignerSerializationVisibility attribute will prevent the designer to serialize the TableStyles property.

Second, add the following to your derived DataGrid:

public override ISite Site
{
get
{
return base.Site;
}
set
{
base.Site = value;
Site.Container.Remove(defaultTableStyle);
}
}

Because the DataGridTableStyle is a component the designer will serialize it altough its not used (by the TableStyle property) so after the Site of the DataGrid is set you must remove your table style (here defaultTableStyle) from the components. Probably you will also need to override Dispose and dispose your table style in there but i'm not sure about that.

This is ugly and I'm not sure it's quite correct but it seems to work...

MikeDanes at 2007-9-9 > top of Msdn Tech,Windows Forms,Windows Forms General...