windowsforms-119885-controls-and-libraries-property-grid.md
The PropertyGridControl emulates the UI found in Visual Studio’s Properties window and in settings panels in Microsoft Office applications. You can assign any Object to the control to allow users to edit that object’s public properties.
Tip
The Property Grid control inherits base functionality from the Vertical Grid Control. Read the following topics for information:
If you assign an object to the PropertyGridControl.SelectedObject property, the control does two things:
You can use the PropertyGridControl.SelectedObjects property to assign an array of objects to the control. The control displays only those properties that apply to all objects in this array.
The example below displays a product list in the LookUpEdit control. The Property Grid displays the selected product’s properties.
//Bind the lookup editor to a data source.
lookUpEdit1.Properties.DataSource = initSampleData();
lookUpEdit1.Properties.DisplayMember = "Name";
lookUpEdit1.Properties.EditValueChanged += Properties_EditValueChanged;
//Assign the selected object to the Property Grid.
private void Properties_EditValueChanged(object sender, EventArgs e) {
propertyGridControl1.SelectedObject = (Product)lookUpEdit1.EditValue;
}
//Create a list of products.
public List<Product> initSampleData() {
List<Product> result = new List<Product>();
result.Add(new Product("FC GTX FE", "Fullcover GPU Blocks", 95.80f, "Nickel"));
//. . .
return result;
}
//Create a business object.
public class Product {
public Product(string name, string category, float price, string color) {
Name = name; Category = category; Price = price; Color = color;
}
public string Name { get; set; }
public string Category { get; set; }
public float Price { get; set; }
public string Color { get; set; }
}
'Bind the lookup editor to a data source.
lookUpEdit1.Properties.DataSource = initSampleData()
lookUpEdit1.Properties.DisplayMember = "Name"
AddHandler lookUpEdit1.Properties.EditValueChanged, AddressOf Properties_EditValueChanged
'Assign the selected object to the Property Grid.
private void Properties_EditValueChanged(Object sender, EventArgs e)
propertyGridControl1.SelectedObject = CType(lookUpEdit1.EditValue, Product)
'Create a list of products.
public List(Of Product) initSampleData()
Dim result As New List(Of Product)()
result.Add(New Product("FC GTX FE", "Fullcover GPU Blocks", 95.80F, "Nickel"))
'. . .
Return result
'Create a business object.
public class Product
public Product(String name, String category, Single price, String color)
Name = name
Category = category
Price = price
Color = color
public String Name {get;set;}
public String Category {get;set;}
public Single Price {get;set;}
public String Color {get;set;}
The PropertyGridControl.ActiveViewType property specifies the Property Grid’s view:
Office — an Office-inspired view. See the following help topic for more information: Office View.
Classic — a Visual Studio-inspired view. See the following help topic for more information: Classic View.
The selected object’s properties may specify their descriptions (DescriptionAttribute). You can use the PropertyDescriptionControl to display these descriptions in your application.
Use the PropertyDescriptionControl.PropertyGrid property to bind the PropertyGridControl to a PropertyDescriptionControl.
The control supports a toolbar as in Visual Studio’s Properties window. The toolbar displays a search box and buttons that allow users to sort and categorize grid rows.
You can use the Property Grid control’s smart tag menu to create the toolbar.
The Property Grid has much in common with the WinForms Vertical Grid control. Read the following topics for information on data editing and input validation:
See Also