Question on UserControl

D

Dom

I've created a UserControl comprised of two controls, a DataGridView
and a DateTimePicker. I've done this because one of the columns in
the DataGridView will hold a date, and when the user clicks on the
column, the DateTimePicker will appear.

Now for the problem.

I want the main program to add columns (and other properties) to the
DataGridView control. So I exposed the control, like this (where
dgvMain is the name of the DataGridView control in the UserControl):

public DataGridView GridControl
{
get {return dgvMain;}
}

This is fine. The properties box of the UserControl now has
"GridControl" which opens to the usual properties of a datagridview.
But when I click on "Columns" in the properties box, I get the message
"Object reference not set to an instance of an object." The odd thing
is, I can add columns in code. (Or maybe that's not so odd).

How do I handle this?

Dom
 
I

Ignacio Machin ( .NET/ C# MVP )

I've created a UserControl comprised of two controls, a DataGridView
and a DateTimePicker.  I've done this because one of the columns in
the DataGridView will hold a date, and when the user clicks on the
column, the DateTimePicker will appear.

Now for the problem.

I want the main program to add columns (and other properties) to the
DataGridView control.  So I exposed the control, like this (where
dgvMain is the name of the DataGridView control in the UserControl):

public DataGridView GridControl
{
     get {return dgvMain;}

}

This is fine.  The properties box of the UserControl now has
"GridControl" which opens to the usual properties of a datagridview.
But when I click on "Columns" in the properties box, I get the message
"Object reference not set to an instance of an object."  The odd thing
is, I can add columns in code.  (Or maybe that's not so odd).

How do I handle this?

Dom

Hi,

Not that's not odd at all :)

you can use it from code with no problem, if you want to use the
designer well, it's a completely different history, you need to
implement at least one interface.
Take a look at MSDN or this article
http://www.eggheadcafe.com/community/aspnet/2/10013886/user-control-properties-i.aspx
 

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