How to add a new item into ObjectListView?

2019-01-28 01:15发布

I tried the demo code in demo project but I can't add new item successfully. It just add new new NULL group and NULL item. Please give me an simple example code to add new item (text and image).

Thank you!


Oh sorry! I forgot it. This is the first time I participate in this site. I use C#. And the code is:

objectListView1.BeginUpdate();
objectListView1.AddObject(new string [] {"Hello","dfdsF" });
objectListView1.EndUpdate();

and

objectListView1.BeginUpdate();
OLVListItem item = new OLVListItem(new string [] {"Hello","dfdsF" });
objectListView1.Items.Add(item);
objectListView1.EndUpdate();

It's so different form ListView and EXListView which I can define a text or a image when creating new item. But in ObjectListView, I don't understand OBJECT?

I get ObjectListView anh it's demo code form here http://nchc.dl.sourceforge.net/project/objectlistview/objectlistview/v2.5/ObjectListViewFull-2.5.0.zip

2条回答
叼着烟拽天下
2楼-- · 2019-01-28 02:05

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

enter image description here

Here's the result:

enter image description here

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 :)

查看更多
Rolldiameter
3楼-- · 2019-01-28 02:14

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.

查看更多
登录 后发表回答