-3

I have a class and few properties assigned to it.

class MyClass
{
        [Category("Common")]
        [Description("Name")]
        [Browsable(true)]
        public string Name
        {
            get { return name;} 
            set { name = value; }
        }

        [Category("Common")]
        [Description("Contact")]
        [Browsable(true)]
        public string ContactNo
        {
            get { return number;} 
            set { number = value; }
        }
}

Here, When I instantiate this class, I want to remove contactNo property when a certain condition is met. How can I do that?

user4818954
  • 11
  • 1
  • 4
  • What do you mean with remove the property? – Mivaweb May 21 '15 at 08:33
  • You can't "remove" properties... What are you trying to do? – xanatos May 21 '15 at 08:33
  • @Mivaweb : I mean I don't want to display that property in property grid. – user4818954 May 21 '15 at 08:35
  • @xanatos : I want to enable/disable property display when this class is instantiated. Is there any way to do that? – user4818954 May 21 '15 at 08:36
  • 1
    @user4818954 You do it at the graphical interface level normally, not at the class level, so you have to specify how you are going to show it. – xanatos May 21 '15 at 08:37
  • 1
    @user4818954 You can use grid column visible false property, If you don't want to show it. Anyway what grid are you using. Add some grid code – Rahul Nikate May 21 '15 at 08:40
  • 2
    Create an abstract class with properties that every class will have after that derive some classes from the abstract class with other properties that can be different from each other. Then based on the condition create an instance of the correct class – Mivaweb May 21 '15 at 08:40
  • It's impossible to help because you provide no relevant information. Since the question is about your UI, you should post the code you use to display your data and ask exactly what you want - *How to display specific properties in the grid control you use*. Don't forget to mention whether you are working on a WinForms, WPF or ASP.NET project – Panagiotis Kanavos May 21 '15 at 08:41
  • possible duplicate of [Programatically Hide Field in PropertyGrid](http://stackoverflow.com/questions/626803/programatically-hide-field-in-propertygrid) – Stephan B May 21 '15 at 08:42

3 Answers3

0

Your approach depends on whether the property should be hidden for all rows in the table (i.e. if you want to hide the entire column or just the values in certain cells).

If you just want to hide the values in certain cells, you can add conditions to the set part of the property to check the condition and return null or an empty string.. If the condition requires a check of an external value, you could have a Get method which would accept the appropriate values to check.

At a UI level, to hide the entire column it really depends on the technology you are using. Which you haven't mentioned here - for browser based apps JQuery is probably the most common tool for this.

amcdermott
  • 1,565
  • 15
  • 23
0

The first step will be creating an abstract class with properties that every class will have.

For instance:

public abstract class ClassRoot {
    public string Name
    {
        get; set;
    }
}

Now you will create derived classes with other properties that can be different from each other.

For instance:

public class Class1 : ClassRoot {
    public string ContactNo
    {
        get; set;
    }
}

Another class may look like this:

public class Class2 : ClassRoot {
    public string Address
    {
        get; set;
    }
}

Now based on your condition you can do something like:

if(....)
{
    Class1 class1 = new Class1();
} else {
    Class2 class2 = new Class2();
}

Every class Class1 or Class2 has the property Name, but contains also their own defined properties.

Mivaweb
  • 5,580
  • 3
  • 27
  • 53
-1
class MyClass
{
    [Category("Common")]
    [Description("Name")]
    [Browsable(true)]
    public string Name
    {
        get { return name;} 
        set { name = value; }
    }

    [Category("Common")]
    [Description("Contact")]
    [Browsable(true)]
    public string ContactNo
    {
        get { return number;} 
        set { number = value; }
    }

    public bool ContactNoVisible
    {
        //Change the condition
        get {return Name != "bttb"; }
    }
}

from now on just check if ContactNoVisible to show the property on grid. If you show me your code how you show the porperty value then i help for that manner too.

brtb
  • 2,201
  • 6
  • 34
  • 53