views:

856

answers:

6

I generate all my columns in a subclassed DataGridView programmatically. However Visual Studio 2008 keeps reading my constructor class (which populates a DataTable with empty content and binds it to the DataGridView) and generates code for the columns in the InitializeComponent method - in the process setting AutoGenerateColumns to false.

This causes errors in design-time compilation which are only solved by manually going into the design code and deleting all references to these autogenerated columns.

How can I stop it doing this?

I have tried:

  • Making the control 'Frozen'
  • Setting the DataGridView instantiated object protected (suggested in a previous post which referred to this site)
+1  A: 

It sounds like you are adding controls in the constructor. Perhaps add the columns slightly later - perhaps something like overriding OnParentChanged; you'll then be able to check DesignMode so you only add the columns during execution (not during design).

Marc Gravell
This worked for me, see answer
Brendan
+1  A: 

I've seen this behavior before for ComboBox's with the Items property and it's really frustrating. Here's how I've gotten around it with ComboBox. You should be able to apply this to the DataGridView.

I created a "new" property called Items and set it to not be browsable and to be explicitly be hidden from serialization. Under the hood it just accesses the real Items property.

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new ObjectCollection Items
{
    get { return ((ComboBox)this).Items; }
}

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new object DataSource
{
    get { return ((ComboBox)this).DataSource; }
JaredPar
I see what you are trying however when I try to adapt this to a subclassed DataGridView it fails. Should there be 'override' in there?
Brendan
You'll have to customize it for DataGridView's Columns. This is just a cut/paste from a ComboBox sample I have. Definately don't want to override as nothing here is virtual
JaredPar
A: 

I do this often in my custom controls, if you wrap the code you don't want to execute in the designer in a DesignMode check, it should fix your problems.

    if (!DesignMode)
    {
        // Your code here
    }
robby valles
I wrapped the code where I populate the DataTable and Bind it to the DataGridView in the above but unfortunately it did not work
Brendan
A: 
Brendan
A: 

I ran into a problem similar to this, and am posting my solution here since when I wrote my question this was the top suggested question. Each time I compiled my code the designer would add each of the columns in the datasource automatically (and the next time I built my code they'd appear in the running app) despite autogenerate columns being set to false.

Eventually I managed to stop it by giving one of my columns the same name that the auto-generated column had (my columns had originally been created manually before the datasource was available).

Dan Neely
A: 

JaredPar's suggestion worked for me:

public partial class RefusjonsOppgjorGrid : DataGridView
{
    public RefusjonsOppgjorGrid()
    {
        InitializeComponent();
    }

    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new DataGridViewColumnCollection Columns
    {
        get{ return base.Columns;}
    }
}
ArildF