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

How can I get row object values of selected row in ObjectListView?

我在 winforms 应用程序中使用 ObjectListView,我使用了整行 select 选项,每当我 select 一行时,我想要该行对象的详细信息(每行创建为目的) 这是我到目前为止创建的方式..

    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()"

假设您选择了一个对象,然后 ObjectListView 提供以下内容:

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

然后您可以检查对象的类型是否正确并对其进行转换。示例如下:

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

编辑:由于我写了这篇文章,您添加了代码以表明您正在使用 TreeListVew,因此概念相似,但我不知道它是否是完全相同的代码。