Click here to Skip to main content
15,914,010 members
Home / Discussions / C#
   

C#

 
GeneralRe: when using Stored Procedure called from C# ? Pin
Member 245846721-Jun-14 14:02
Member 245846721-Jun-14 14:02 
GeneralRe: when using Stored Procedure called from C# ? Pin
Member 245846721-Jun-14 14:04
Member 245846721-Jun-14 14:04 
AnswerRe: when using Stored Procedure called from C# ? Pin
ZurdoDev20-Jun-14 2:01
professionalZurdoDev20-Jun-14 2:01 
GeneralRe: when using Stored Procedure called from C# ? Pin
Member 245846720-Jun-14 23:05
Member 245846720-Jun-14 23:05 
AnswerRe: when using Stored Procedure called from C# ? Pin
ZurdoDev21-Jun-14 2:15
professionalZurdoDev21-Jun-14 2:15 
GeneralRe: when using Stored Procedure called from C# ? Pin
Member 245846721-Jun-14 13:58
Member 245846721-Jun-14 13:58 
AnswerRe: when using Stored Procedure called from C# ? Pin
ZurdoDev21-Jun-14 15:38
professionalZurdoDev21-Jun-14 15:38 
GeneralRe: when using Stored Procedure called from C# ? Pin
Member 245846722-Jun-14 18:28
Member 245846722-Jun-14 18:28 
QuestionRectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
BillWoodruff19-Jun-14 14:39
professionalBillWoodruff19-Jun-14 14:39 
AnswerRe: RectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
OriginalGriff19-Jun-14 21:14
mveOriginalGriff19-Jun-14 21:14 
GeneralRe: RectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
BillWoodruff19-Jun-14 23:28
professionalBillWoodruff19-Jun-14 23:28 
GeneralRe: RectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
OriginalGriff19-Jun-14 23:37
mveOriginalGriff19-Jun-14 23:37 
QuestionRe: RectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
Richard MacCutchan20-Jun-14 1:33
mveRichard MacCutchan20-Jun-14 1:33 
QuestionBinding two ComboBoxes Pin
sbn101019-Jun-14 9:18
sbn101019-Jun-14 9:18 
AnswerRe: Binding two ComboBoxes Pin
Eddy Vluggen20-Jun-14 8:06
professionalEddy Vluggen20-Jun-14 8:06 
Questionextract data from excel Pin
solo919-Jun-14 8:37
solo919-Jun-14 8:37 
AnswerRe: extract data from excel Pin
Richard Deeming19-Jun-14 8:54
mveRichard Deeming19-Jun-14 8:54 
AnswerRe: extract data from excel Pin
CatchExAs19-Jun-14 12:31
professionalCatchExAs19-Jun-14 12:31 
AnswerRe: extract data from excel Pin
Richard MacCutchan19-Jun-14 22:53
mveRichard MacCutchan19-Jun-14 22:53 
QuestionChange Properties Category during runtime for PropertyGrid Pin
Mc_Topaz19-Jun-14 4:12
Mc_Topaz19-Jun-14 4:12 
I have been trying to change the Category field of a property displayed in a PropertyGrid in runtime.

I know how to change DisplayName and Browseable in runtime and I thought it would be the same way. Just modifying the code. But nope...

The class I want to display in the PropertyGrid is this:
C#
public class Person
{
    int age = 10;

    [DisplayName("Age")]
    [Category("Fact")]
    public int Age
    {
        get { return age; }
    }
}


I have a Windows form application containing a PropertyGrid (propertyGrid1) and a button (button1) to switch Person.Age's category between "Fact" and "Info".
C#
public Form1()
{
    InitializeComponent();

    propertyGrid1.SelectedObject = new Person();
}

private void button1_Click(object sender, EventArgs e)
{
    SetCategory();
    SetDisplayName();
}


To switch the DisplayName I have this method:
C#
private void SetDisplayName()
{
    Person person = propertyGrid1.SelectedObject as Person;

    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(person.GetType())["Age"];
    DisplayNameAttribute nameAttribute = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
    FieldInfo displayName = nameAttribute.GetType().GetField("_displayName", BindingFlags.NonPublic | BindingFlags.Instance);

    string name = displayName.GetValue(nameAttribute).ToString() == "Age" ? "The age" : "Age";
    displayName.SetValue(nameAttribute, name);

    propertyGrid1.SelectedObject = person; // Mandetory or the DisplayName won't change in PropertyGrid...
}

This works well.

I "copied" the SetDisplayName method and made a SetCategory method like this:
C#
private void SetCategory()
{
    Person person = propertyGrid1.SelectedObject as Person;

    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(person.GetType())["Age"];
    CategoryAttribute categoryAttribute = (CategoryAttribute)descriptor.Attributes[typeof(CategoryAttribute)];
    FieldInfo category = categoryAttribute.GetType().GetField("categoryValue", BindingFlags.NonPublic | BindingFlags.Instance);

    string text = category.GetValue(categoryAttribute).ToString() == "Fact" ? "Info" : "Fact";

    category.SetValue(categoryAttribute, text);
    propertyGrid1.SelectedObject = person;
}

This works all well. When the category.SetValue() has been executed the intelisence of categoryAttribute show that the Category property is "Info" and not "Fact" as previous. Just as I want it.
When the method is finished the category of Person.Age has not been changed and the propertyGrid1 still show "Fact" as category.

I have tried to make any sense of this article which show that it's possible: Dynamic Properties for PropertyGrid[^]
But that example is to big to scope and I get confused.

Can anyone help me?

/Steffe
AnswerRe: Change Properties Category during runtime for PropertyGrid Pin
Bernhard Hiller19-Jun-14 20:49
Bernhard Hiller19-Jun-14 20:49 
RantRe: Change Properties Category during runtime for PropertyGrid Pin
Mc_Topaz19-Jun-14 21:17
Mc_Topaz19-Jun-14 21:17 
RantRe: Change Properties Category during runtime for PropertyGrid Pin
Eddy Vluggen20-Jun-14 8:22
professionalEddy Vluggen20-Jun-14 8:22 
QuestionNon-XML ini-file supporting arrays, in plain text format? Pin
arnold_w19-Jun-14 3:30
arnold_w19-Jun-14 3:30 
AnswerRe: Non-XML ini-file supporting arrays, in plain text format? Pin
V.19-Jun-14 4:07
professionalV.19-Jun-14 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.