Hiding a property from grid at run time based on some condition
- Hi,
I want to hide property at run time from grid based on only if property value is null. I can't use Browsable attribute to a property of a class as this always hides. Could anyone help me in this.
Answers
- There is a great article on CodeProject that answers your question, you can find it here: Unleash PropertyGrid with Dynamic Properties and Globalization .
while(!Asleep) sheep++;- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:46 AM
Hi Harish,
it's not very clear to me what you want to do, but it looks like you're trying to crop Property_X out of the base class A instead of creating a proper base class A (without Property_X) and then deriving a class B containing Property_X.
If the nulling of the property is of such importance that you consider to show/hide the property in the UI at runtime maybe you should think of changing your class design.
Here is a very simple example of how I would do it:public partial class Form1 : Form { private IPerson[] m_Persons = new IPerson[2]; public Form1() { InitializeComponent(); m_Persons[0] = new Person() { FirstName = "John", LastName = "Doe" }; m_Persons[1] = new Employee( m_Persons[0], "101"); } private void button1_Click(object sender, EventArgs e) { // Randomly select a Person/Employee object for display int idx = new Random().Next(0, 2) == 0 ? 0 : 1; propertyGrid1.SelectedObject = m_Persons[idx]; } }
And here are the classes:internal interface IPerson { string FirstName { get; set; } string LastName { get; set; } } class Person : IPerson { public string FirstName { get; set; } public string LastName { get; set; } } class Employee : Person { public Employee() : this(null, null) { } public Employee(string employeeId) : this(null, employeeId) { } public Employee(IPerson person, string employeeId) { base.FirstName = person.FirstName; base.LastName = person.LastName; EmployeeId = employeeId; } public string EmployeeId { get; set; } }
Hope this helps.
Marcel- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:46 AM
All Replies
- There is a great article on CodeProject that answers your question, you can find it here: Unleash PropertyGrid with Dynamic Properties and Globalization .
while(!Asleep) sheep++;- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:46 AM
Hi Harish,
it's not very clear to me what you want to do, but it looks like you're trying to crop Property_X out of the base class A instead of creating a proper base class A (without Property_X) and then deriving a class B containing Property_X.
If the nulling of the property is of such importance that you consider to show/hide the property in the UI at runtime maybe you should think of changing your class design.
Here is a very simple example of how I would do it:public partial class Form1 : Form { private IPerson[] m_Persons = new IPerson[2]; public Form1() { InitializeComponent(); m_Persons[0] = new Person() { FirstName = "John", LastName = "Doe" }; m_Persons[1] = new Employee( m_Persons[0], "101"); } private void button1_Click(object sender, EventArgs e) { // Randomly select a Person/Employee object for display int idx = new Random().Next(0, 2) == 0 ? 0 : 1; propertyGrid1.SelectedObject = m_Persons[idx]; } }
And here are the classes:internal interface IPerson { string FirstName { get; set; } string LastName { get; set; } } class Person : IPerson { public string FirstName { get; set; } public string LastName { get; set; } } class Employee : Person { public Employee() : this(null, null) { } public Employee(string employeeId) : this(null, employeeId) { } public Employee(IPerson person, string employeeId) { base.FirstName = person.FirstName; base.LastName = person.LastName; EmployeeId = employeeId; } public string EmployeeId { get; set; } }
Hope this helps.
Marcel- Marked As Answer byeryangMSFT, ModeratorMonday, November 09, 2009 6:46 AM
- Hi Harish,
Are those replies helpful for you? It will be very beneficial for other community members having the similar questions if you mark useful replies as answers.Thanks,
Eric
Please remember to mark helpful replies as answers and unmark them if they provide no help.


