【问题标题】:How to add a new item into ObjectListView?如何将新项目添加到 ObjectListView?
【发布时间】:2011-10-31 03:59:30
【问题描述】:

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

谢谢!


哦,对不起!我忘记了。这是我第一次参加这个网站。 我使用 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 anh 这是演示代码表单http://nchc.dl.sourceforge.net/project/objectlistview/objectlistview/v2.5/ObjectListViewFull-2.5.0.zip

【问题讨论】:

  • 这是什么语言的?演示项目的源代码是什么?为了帮助您,我们需要更多信息。请在此处查看常见问题解答,了解有关如何提问的提示。
  • 对不起!我忘记了。我使用 C#。代码是:objectListView1.BeginUpdate(); objectListView1.AddObject(new string [] {"Dfdf","dfdsF" }); objectListView1.EndUpdate();我得到了演示示例表单:sourceforge.net/projects/objectlistview/files/latest/download
  • 最好的办法是查看为此控件提供的详细入门文档:Getting Started 这里还有更深入的问题和答案:Learning to cook 以及 CodeProject 上的详细教程:A Much Easier to Use ListView

标签: objectlistview


【解决方案1】:

我将向您展示如何添加项目。尝试创建一个类,然后为要在 ObjectListView 上显示的属性创建 getter 和 setter。

SetObjects 方法采用List<T>:

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

现在这是我的课程,我将其命名为 haha,其中有两个属性(Name 和 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;
    }
}

现在

  • 将ObjectListView 中的ShowGroups 更改为false
  • 然后添加您想要的列;我添加了两列,一列用于Name,另一列用于Detail
  • 如图所示,当您添加一列时,请查看 AspectName 并写下您希望在类中显示的属性的完全相同的名称

结果如下:

如果你想使用AddObject(),它接受一个对象,我会这样写:

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

编码愉快:)

【讨论】:

    【解决方案2】:

    最好的办法是使用实​​体类。然后制作一个项目列表并将此列表添加到您的ObjectListView。

    myObjectListView.SetObjects(myListofEntityItems);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多