.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > Hiding a property from grid at run time based on some condition
Ask a questionAsk a question
 

AnswerHiding a property from grid at run time based on some condition

  • Monday, November 02, 2009 8:44 AMHarish T Badagi Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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

  • Monday, November 02, 2009 8:56 AMPieter Joost van de SandeMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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++;
  • Monday, November 02, 2009 1:04 PMMarcel Roma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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

All Replies

  • Monday, November 02, 2009 8:56 AMPieter Joost van de SandeMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    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++;
  • Monday, November 02, 2009 1:04 PMMarcel Roma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code

    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

  • Friday, November 06, 2009 7:04 AMeryangMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.