3

I have an Employee class. There are many departments maintained in the database and an employee may only belong to a particular department.

public class Employee
{
    private string name;
    private int depID;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public int DepartmentID
    {
        get { return depID; }
        set { depID = value; }
    }
}

public class Department
{
    private int depID;
    private string depName;

    public int DepartmentID
    {
        get { return depID; }
    }

    public int DepartmentName
    {
        get { return depName; }
        set { depName = value; }
    }
}

How can I display the object Employee in the PropertyGrid with the department as a one of the properties that will be displayed as combobox?

Is it possible? Or is there any better implementation? Thanks in advance for your inputs.

KreepN
  • 8,528
  • 1
  • 40
  • 58
yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • When you say *any better implementation*, you mean you may renounce the PropertyGrid? A ComboBox in a PropertyGrid is not as straightforward as it may look, so any other solution can hardly be worse. – Gert Arnold Sep 23 '11 at 15:38

2 Answers2

4

I went ahead and drafted you up an experiment (for myself as well since I have never done this). It uses Linq for this particular solution to populate the combo box at hand, but I'm sure you could populate it other ways as well.

My documentation came from here under the subsection Adding Domain List and Simple Drop-down Property Support

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;

public class Employee : StringConverter
{
    DataClasses1DataContext mydb = new DataClasses1DataContext();

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
    {
        var a = (from u in mydb.Customers select u.CustomerID).ToArray();
        return new StandardValuesCollection(a);
    }

    public string Name { get; set; }

    [TypeConverter(typeof(Employee)), CategoryAttribute("Document Settings")]
    public string DepartmentID { get; set; }
}

On the form load I selected:

 private void Form1_Load(object sender, EventArgs e)
 {
     Employee temp = new Employee();
     propertyGrid1.SelectedObject = temp;
 }

I hope this is what you are looking for. It's worth noting that you may change StringConverter to TypeConverter if you'd like, but I used String becasue the field I'm dealing with is a string.

enter image description here

KreepN
  • 8,528
  • 1
  • 40
  • 58
  • Alt-PrntScrn will copy just the active window to the clipboard, not the entire desktop. – LarsTech Sep 23 '11 at 19:21
  • @ Lars Pro tip is appreciated :) – KreepN Sep 23 '11 at 19:28
  • I personally think it is an anti pattern to let a view implementation trickle down into a domain model, which `Employee` seems to be part of to me. It may be right if `Employee` is a view model or DTO, but then I would use a different naming convention. – Gert Arnold Sep 24 '11 at 23:12
  • this is quite overkill for me : ). I'm not yet familiar with LINQ. Is there a non-linq way to achieve this? Yes that's what I'm trying to achieve based from your screenshot : ) – yonan2236 Sep 26 '11 at 11:14
  • You don't have to use linq if you do not wish to, you could use SQL via C# or even create a table adapter and read from there. The only requirement you see in the example above is that the variable a is a string array. What feeds that array is up to you. – KreepN Sep 26 '11 at 13:42
2

You can achieve that by implementing the TypeConverter

Krishna
  • 688
  • 1
  • 7
  • 10