如何在ObjectListView中获取所选行的行对象值?

问题描述 投票:0回答:1

我在winforms应用程序中使用ObjectListView,我使用了全行选择选项,每当我选择一行时,我都希望该行对象的详细信息(每行都像一个对象一样创建)这是我到目前为止创建的方式。

    class Node
                    {
                        public string Name { get; private set; }
                        public string StartTime { get; private set; }
                        public string  EndTime  { get; private set; }
                        public string tag;
                        public List<Node> Children { get; private set; }
                        public Node(string name, string col1, string col2, string col3, string col4)
                        {
                            this.Name = name;
                            this.StartTime = col1; 
                            this.EndTime = col2;
                            this.Children = new List<Node>();
                        }
                    }

        public MainForm()
                {
         treeListView = new BrightIdeasSoftware.TreeListView();
                    Result.Controls.Add(treeListView);
        treeListView.CanExpandGetter = x => (x as Node).Children.Count > 0;
                    // set the delegate that the tree uses to know the children of a node
                    treeListView.ChildrenGetter = x => (x as Node).Children;

                    // create the tree columns and set the delegates to print the desired object proerty
                    var nameCol = new BrightIdeasSoftware.OLVColumn("Name", "Name");
                    nameCol.AspectGetter = x => (x as Node).Name;

                    var col1 = new BrightIdeasSoftware.OLVColumn("StartTime", "StartTime");
                    col1.AspectGetter = x => (x as Node).StartTime;

                    var col2 = new BrightIdeasSoftware.OLVColumn("EndTime", "EndTime");
                    col2.AspectGetter = x => (x as Node).EndTime;
                    // add the columns to the tree
                    treeListView.Columns.Add(nameCol);
                    treeListView.Columns.Add(col1);
                    treeListView.Columns.Add(col2);
                    treeListView.FullRowSelect = true;

        var parent1 = new Node("PARENT1", "-", "-", "-");
                    parent1.Children.Add(new Node("CHILD_1_1", "A", "X", "1"));
                    parent1.Children.Add(new Node("CHILD_1_2", "A", "Y", "2"));
                    parent1.Children.Add(new Node("CHILD_1_3", "A", "Z", "3"));

                    var parent2 = new Node("PARENT2", "-", "-", "-");
                    parent2.Children.Add(new Node("CHILD_2_1", "B", "W", "7"));
                    parent2.Children.Add(new Node("CHILD_2_2", "B", "Z", "8"));
                    parent2.Children.Add(new Node("CHILD_2_3", "B", "J", "9"));

                    var parent3 = new Node("PARENT3", "-", "-", "-");
                    parent3.Children.Add(new Node("CHILD_3_1", "C", "R", "10"));
                    parent3.Children.Add(new Node("CHILD_3_2", "C", "T", "12"));
       data = new List<Node> { parent1, parent2, parent3 };
     treeListView.Roots = data; 
    treeListView.CellClick += (sender2, e2) => selectedRow(sender2, e2);

            }
        private void selectedRow(object sender2, CellClickEventArgs e2)
            {
                object v2 = treeListView.SelectedObject();
             //here i want columns data of selected row, but i can't get using v2 object
            }

}

我不知道我是否使用正确的函数“ CellClick()”

c# .net objectlistview
1个回答
0
投票

假设您已选择一个对象,则ObjectListView提供以下内容:

  • SelectedItem->单个OLVListItem
  • SelectedItems-> OLVListItem的集合
  • SelectedObject->单个对象
  • SelectedObjects->对象的IList

然后您可以检查对象是否正确,然后进行转换。下面是一个示例:

if (objectListView1.SelectedItems.Count > 0)
{
    MyObject obj = objectListView1.SelectedObject as MyObject;
    if (obj != null)
    {
       //Work on your object here.
    }
}

编辑:自从我写这篇文章以来,您添加了代码以显示您正在使用TreeListVew,因此概念相似,但是我不知道它是否完全相同。

© www.soinside.com 2019 - 2024. All rights reserved.