문제

I'm trying to populate a DataTreeListView from ObjectListView library using a list. Unfortunately I am unable to achieve it, there is nothing displayed even though there is a count of item inside the List itself.

 Class.cs

 public class Class
 {
   protected string xName;
   protected int xId;
   protected int xParentId;
   protected int happinessStatus;
   protected int salaryStatus;

  public Class()
  {
  this.xName = "";
  this.xId = 0;
  this.xParentId = 0;
  this.happinessStatus = 0;
  this.salaryStatus = 0;
  }

  public String Name 
  {
    get { return this.xName; }
    set { this.xName = value; }
  }

  public int Id
  {
    get { return this.xId; }
    set { this.xId = value; }
   }

  public int ParentId
  {
   get { return this.xParentId; }
   set { this.xParentId = value; }
  }

  public int HappinessStatus
  {
    get {return this.happinessStatus; }
    set { this.happinessStatus = value; }
  }

  public int SalaryStatus
  {
    get { return this.salaryStatus; }
    set { this.salaryStatus = value; }
  }

  public static List<Class> GetList()
  {
     List<Class> oList = new List<Class>();
     Class oClass = new Class();

     oClass.Name = "Person A";
     oClass.Id = 1;
     oClass.ParentId = 0;
     oClass.HappinessStatus = 1;
     oClass.SalaryStatus = 1000;
     oList.Add(oClass);

     oClass.Name = "Person B";
     oClass.Id = 2;
     oClass.ParentId = 1;
     oClass.HappinessStatus = 1;
     oClass.SalaryStatus = 2000;
     oList.Add(oClass);

     oClass.Name = "Person C";
     oClass.Id = 3;
     oClass.ParentId = 1;
     oClass.HappinessStatus = 1;
     oClass.SalaryStatus = 1000;
     oList.Add(oClass);

     return oList; 
    }

On the MainForm's Load Event,

I did the following:

    List<Class> list = new List<Class>();
    list = Class.GetList();
    dataTreeListView1.DataSource = list;

On the designer view, I've also created columns which has got aspect name set to each of the property of the class file except the Id and ParentId.

KeyAspectName : Id ParentKeyAspectName: ParentId

I did a small messagebox to show the count of the item in the list, its correct but nothing displayed out on the dataTreeListView control.

May I know what is wrong with my coding?

도움이 되었습니까?

해결책

Did you set KeyAspectName, ParentKeyAspectName and RootKeyValue accordingly?

If you did it using the designer, RootKeyValue may be your problem:

Due to the limitations of the Designer in the IDE, RootKeyValue can only be given a string value through the IDE. If your ParentKey is not of type string, you will have to set its value through code.

Since you parent key is of type int use

dataTreeListView1.RootKeyValue = 0;

Note that in contrast to the basic OLV, you don't need to add columns manually. If you want to hide the key columns set ShowKeyColumns = false.

EDIT: There is another mistake in you code. You add the same instance of the object oClass 3 times. Use oClass = new Class(); before initializing a new person.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top