How to add a new item into ObjectListView?

你离开我真会死。 提交于 2019-11-29 14:08:50

I will show you what to do to add items. Try to create a class, then make getters and setters for the properties you want to show on your ObjectListView.

SetObjects method takes a List<T>:

public Form1()
{
    InitializeComponent();
    this.objectListView1.SetObjects(haha.GET());
}

Now this is my class, I called it haha, I've two properties in it (Name and Detail):

class haha
{
    string name;
    string detail;
    public haha(string name , string detail)
    {
        this.name = name;
        this.detail = detail;
    }

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

    static internal List<haha> GET()
    {
        haha item = new haha("zeko", "dunno");
        haha xx = new haha("sheshe", "dunno");
        haha ww = new haha("murhaf", "dunno");
        haha qq = new haha("soz", "dunno");
        haha ee = new haha("HELLO", "dunno");
        List<haha> x = new List<haha>();
        x.Add(item);
        x.Add(xx);
        x.Add(ww);
        x.Add(qq);
        x.Add(ee);
        return x;
    }
}

Now

  • change ShowGroups in ObjectListView to false
  • then add the columns that you want; I've added two columns, one for Name and one for Detail
  • and as in the picture when you add a column, see the AspectName and write exactly the same name of its property that you want to show from your class

Here's the result:

If you want to use AddObject(), which takes an object, I'd write this:

private void button1_Click(object sender, EventArgs e)
{
    haha newObject = new haha("memo","zezo");
    objectListView1.AddObject(newObject);
}

Happy coding :)

Florian Leitgeb

The best thing is to use an entity class. Then make a list of items and add this list to your ObjectListView.

myObjectListView.SetObjects(myListofEntityItems);

But before you do that, you have to setup the columns in your designer. Just add a column, and in the field AspectName enter the exact name of the attribute of your entity item.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!