How to add a new item into ObjectListView?
Asked Answered
K

2

6

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

Klump answered 31/10, 2011 at 3:59 Comment(3)
What language is this in? What is the source code for the demo project? We need substantially more information in order to help you. Please review the FAQ for tips on how to ask questions here.Tarp
Oh sorry! I forgot it. I use C#. And the code is: objectListView1.BeginUpdate(); objectListView1.AddObject(new string [] {"Dfdf","dfdsF" }); objectListView1.EndUpdate(); I got the demo example form: sourceforge.net/projects/objectlistview/files/latest/downloadKlump
Your best bet is to review the detailed getting started documentation that is provided for this control: Getting Started There are also more in-depth questions and answers here: Learning to cook And a detailed tutorial on CodeProject: A Much Easier to Use ListViewTarp
C
26

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

Corrode answered 28/2, 2012 at 13:57 Comment(0)
L
0

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.

Linguiform answered 6/2, 2013 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.