Read-only PropertyGrid C#
The following example shows how to create readonly property grid in C#.
The .net property gird doesnot allows to make it readonly. Textboxes used to edit values of properties of selected object in property grid are disabled if the property is set as readonly. In this example we are adding the readonly attribute at runtime to all properties of selectedobject.
C# code
——————————
public partial class ReadOnlyPropertyGrid : PropertyGrid
{
private bool isReadOnly;
public ReadOnlyPropertyGrid()
{
InitializeComponent();
}
public bool ReadOnly
{
get { return isReadOnly; }
set
{
isReadOnly = value;
SetObjectAsReadOnly();
}
}
public ReadOnlyPropertyGrid(IContainer container)
{
container.Add(this);
InitializeComponent();
}
protected override void OnSelectedObjectsChanged(EventArgs e)
{
SetObjectAsReadOnly();
base.OnSelectedObjectsChanged(e);
}
private void SetObjectAsReadOnly()
{
if (SelectedObject != null)
{
TypeDescriptor.AddAttributes(SelectedObject, new Attribute[] { new ReadOnlyAttribute(isReadOnly) });
Refresh();
}
}
}






