Discussion:
Right way to expose the Columns property of a DataGridView within a UserControl
(too old to reply)
Patrick B.
2009-06-02 01:42:59 UTC
Permalink
I have a UserControl that contains a DataGridView.

The UserControl exposes the Columns property of the
DataGridView so users can add columns.

However, at design time the "Add Column" dialog
does not appear in the designer when it should.

Here is a simplified version of my code:

public partial class SpringGrid : UserControl
{
public SpringGrid()
{
InitializeComponent();
}
public DataGridViewColumnCollection Columns
{
get { return this.dataGridView.Columns; }
}
}

I found a thread in a dotnet newsgroup where somebody
was having a similar problem. Linda Liu presented a solution.
The thread is here:

http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic57961.aspx

Linda's code was very helpful, but I cannot get the solution to work fully.
After implementing Linda's solution, my code now looks something
like this:

[DesignerSerializer(typeof(MyCodeDomSerializer),
typeof(CodeDomSerializer))]
public partial class SpringGrid : UserControl
{
public SpringGrid()
{
InitializeComponent();
}

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DataGridViewColumnCollection Columns
{
get { return this.dataGridView.Columns; }
}
}

When editing the Columns property of my SpringGrid in the designer,
the DataGridViewColumn Collection Editor appears, but when the
Add button is clicked the Add Column dialog does NOT appear.

Instead, a DataGridViewColumn is automatically added to the collection.
(This is not one of the normal types like a DataGridViewTextBoxColumn.)

I later get the following error message:

At least one of the DataGridView control's columns has no cell template.

Can anyone help?

Thanks!

Patrick
Patrick B.
2009-06-02 08:11:19 UTC
Permalink
Please ignore my previous post to this newsgroup. After working on it
longer, I realized I had overlooked part of Linda's solution. Her
solution works for me now.

For reference, I found another thread pertaining to this subject. If
anyone else bumps into this same problem, the following thread has
useful information:

http://social.msdn.microsoft.com/Forums/en-US/winformsdesigner/thread/c0903eb4-53fa-4881-82ed-1a07fb88dc83

However, Linda's solution is the most comprehensive and useful one I've
found.

Thanks!

Patrick

Loading...