2011-10-31 121 views
5

我尝试了演示项目中的演示代码,但无法成功添加新项目。 它只是添加新的新的NULL组和NULL项目。 请给我一个简单的示例代码添加新项目(文本和图像)。如何将新项目添加到ObjectListView?

谢谢!


对不起!我忘了。这是我第一次参加这个网站。 我使用C#。而且代码:

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

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

它是如此的不同形式的ListView和EXListView其中,创建新项目时,我可以定义一个文本或图像。但在ObjectListView中,我不明白OBJECT?

我得到ObjectListView无水它在这里http://nchc.dl.sourceforge.net/project/objectlistview/objectlistview/v2.5/ObjectListViewFull-2.5.0.zip

+0

这是什么语言?演示项目的源代码是什么?我们需要更多的信息来帮助你。请查阅常见问题解答,了解如何在此提问。 –

+0

对不起!我忘了。我使用C#。 代码如下: objectListView1.BeginUpdate(); objectListView1.AddObject(new string [] {“Dfdf”,“dfdsF”}); objectListView1.EndUpdate(); 我得到了演示示例窗体:http://sourceforge.net/projects/objectlistview/files/latest/download –

+1

您最好打赌的是查看为此控件提供的详细入门文档:[入门]( http://objectlistview.sourceforge.net/cs/gettingStarted.html#gettingstarted)这里还有更深入的问题和答案:[学习烹饪](http://objectlistview.sourceforge.net/cs/recipes。 html#cookbook)和一个关于CodeProject的详细教程:[很容易使用ListView](http://www.codeproject.com/KB/list/ObjectListView.aspx) –

回答

19

演示代码的形式,我会告诉你怎样做才能添加项目。尝试创建一个班级,然后为您想要在ObjectListView上显示的特性制作获取者和设置者。

SetObjects方法采用List<T>

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

现在,这是我的课,我把它叫做haha,我有两个属性在里面(NameDetail):

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; 
    } 
} 

现在

  • 更改ShowGroups in ObjectListView to false
  • 然后添加所需的列;我已经添加了两列,一为Name,一个用于Detail
  • ,并在图片中,当你添加一列,看到AspectName,写它的属性,你想从你的类显示完全相同的名称

enter image description here

这里的结果:

enter image description here

如果你想用AddObject(),这需要一个对象,我会这样写:

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

编码愉快:)

0

最好的办法是用一个实体类。然后制作物品清单并将此清单添加到您的ObjectListView

myObjectListView.SetObjects(myListofEntityItems); 

但是在做之前,您必须在设计器中设置列。只需添加一列,然后在字段AspectName中输入实体项目属性的确切名称。