Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Change the DisplayName of property grid at run-time

$
0
0

Hi,

I am trying to change the DisplayName and Description attributes of property grid dynamically. I changed it for Browsable attribute but wondering why it is not working for DisplayName property. I tried creating Function to change the DisplayName attribute but it is returning null always.

Am I doing anything wrong? I appreciate your help in resolving this.

Below is the code.

public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			UserData ud = new UserData();
			ud.Country = "US";
			ud.State = "California";

			propertyGrid1.SelectedObject = ud;
		}

		public void BrowsableGridProperty(object obj, string propertyName, bool isBrowsable)
		{
			PropertyDescriptor descriptor = TypeDescriptor.GetProperties(obj.GetType())[propertyName];
			BrowsableAttribute attribute = (BrowsableAttribute)
										  descriptor.Attributes[typeof(BrowsableAttribute)];
			System.Reflection.FieldInfo fieldToChange = attribute.GetType().GetField("browsable",
											 System.Reflection.BindingFlags.NonPublic |
											 System.Reflection.BindingFlags.Instance);

			if (fieldToChange != null) fieldToChange.SetValue(attribute, isBrowsable);
			propertyGrid1.Refresh();
		}


		public void DisplayNameGridProperty(object obj, string propertyName, string displayName)
		{
			PropertyDescriptor descriptor = TypeDescriptor.GetProperties(obj.GetType())[propertyName];
			DisplayNameAttribute attribute = (DisplayNameAttribute)
										  descriptor.Attributes[typeof(DisplayNameAttribute)];
			System.Reflection.FieldInfo fieldToChange = attribute.GetType().GetField("displayname",
											 System.Reflection.BindingFlags.NonPublic |
											 System.Reflection.BindingFlags.Instance);

			if (fieldToChange != null) fieldToChange.SetValue(attribute, displayName);
			propertyGrid1.Refresh();
		}
		//hide state property
		private void button1_Click(object sender, EventArgs e)
		{
			BrowsableGridProperty(propertyGrid1.SelectedObject, "State", false);
		}

		//show state property
		private void button2_Click(object sender, EventArgs e)
		{
			BrowsableGridProperty(propertyGrid1.SelectedObject, "State", true);
			DisplayNameGridProperty(propertyGrid1.SelectedObject, "State", "My State");
		}
	}

	public class UserData
	{
		string mCountry = "";
		[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
		[ReadOnly(false)]
		[Browsable(true)]
		public string Country
		{
			get { return mCountry; }
			set { mCountry = value; }
		}

		string mState = "";
		[ReadOnly(true)]
		[Browsable(true)]
		public string State
		{
			get { return mState; }
			set { mState = value; }
		}
	}


Regards, Jignesh Please vote as helpful if my question/answer help you finding your solution.


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>