How to add a new item into ObjectListView?

試著忘記壹切 提交于 2019-12-18 08:30:26

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/7949887/how-to-add-a-new-item-into-objectlistview

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