user386167
user386167

Reputation:

User control's children properties in the Properties window

I have a composite user control containing a DataGridView. In addition, the DataGridView.Modifiers property is set to Public.

When dropping my user control to the designer, I would like to see the properties of this inner DataGridView in the Properties window.

What should I do? Many thanks.

Upvotes: 2

Views: 684

Answers (3)

jross
jross

Reputation: 1119

Even though the other two answers are correct too, I would argue that the more common way is to just allow read-only access to the reference of the instance of the inner grid.

public DataGridView DataGrid { get { return this.dataGridView1; } }

This way the user of your control can access all the grid properties and override whatever defaults you have chosen for it (like background color, etc…) but they cannot replace the grid w/ a new instance, which could possibly mess up some internal logic of your control.

Upvotes: 2

Paul Sasik
Paul Sasik

Reputation: 81489

Your inner DataGridVIew needs to have an instance and a public property defined in the composite control via which it can be accessed.

Something like this:

// your grid control instantiated somewhere:
DataGridVIew myInnerDataGridVIew = new ...

public DataGridVIew MyInnerDataGridVIew 
{ 
  get { return myInnerDataGridVIew; } 
  set { myInnerDataGridVIew = value; } 
}

Once you create this property and rebuild, you should see a MyInnerDataGridVIew member in the properties window when the composite control is selected in the designer, with a [+] next to it. Clicking the plus you should see the DataGridVIew's properties expanded.

Upvotes: 3

Daniel Wardin
Daniel Wardin

Reputation: 1858

What you need to do is add a public property so that any container you drag that control into will see it as a property.

Simply, lets assume you have 1 static data grid which a name of dataGrid1. Now to make that accessible outside (and visible in the properties box) you need to make a property which will set and return it.

public DataGridView ChildDataGridView
{
    get { return this.dataGrid1; }
    set { this.dataGrid1 = value; }
}

That will allow you to modify it from outside. Now when you add the control to the designer you should see a new property called ChildDataGridView when you have your user control selected.

You can modify and access it from that property.

This will only work if the dataGrid1 is there all the time and is not generated dynamically in the code.

Upvotes: 1

Related Questions