Search code examples
c#objectlistviewtreelistview

Update a value in treeListView in objectListView


I have built a treeListView:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeListViewTest1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.treeListView1.CanExpandGetter = delegate(object x) 
            { 
                return true; 
            };
            this.treeListView1.ChildrenGetter = delegate(object x) 
            {
                Contract contract = x as Contract;
                return contrat.Children;
            };

            column1.AspectGetter = delegate(object x)
            {
                if(x is Contract)
                {
                    return ((Contract)x).Name;
                }
                else
                {
                    return " ";
                }
            };

            column2.AspectGetter = delegate(object x)
            {
                if(x is Contract)
                {
                    return ((Contract)x).Value;
                }
                else
                {
                    Double d = (Double)x;
                    return d.ToString();
                }
            };

            this.treeListView1.AddObject(new Contract("A", 1));

        }

        private void treeListView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

    public class Contract
    {
        public string Name { get; set;}
        public Double Value { get; set; }
        public List<Double> Children {get; set;}

        public Contract(string name, Double value)
        {
            Name = name;
            Value = value;
            Children = new List<Double>();
            Children.Add(2);
            Children.Add(3);
        }
    }
}

It gives this output:

Name     Value

A        1

         2

         3

How do I update the values of column2 ("Value") of the parent and the children from within an event? (increasing each value by of the parent and the children.)

private void button1_Click(object sender, EventArgs e)
{

}

I do not understand if I have to use the AspectGetter again or can i modify just the values in column2 somehow and then refreshObjects().


Solution

  • I do not understand if I have to use the AspectGetter again or can i modify just the values in column2 somehow and then refreshObjects().

    No, you should just manipulate the underlying model object and call treeListView1.RefreshObject(myObject); for example, where myObject should be a Contract in you case. This will refresh the contents of the respective rows.

    I don't know what your button1_Click() is supposed to do, but you obviously require a reference of the object you want to refresh. This could be the currently selected object for example (treeListView.SelectedObject). If you tell what you want to do, I may be able to give you more information.