C# extend a listView containing columns with custom object

Viewed 65

Hello I am creating a Windows Form application

I have a custom class called Project.cs with the following properties:

  • Name: String
  • Revenue: int
  • UUID: String
using System;
using System.Collections.Generic;
using System.Text;

namespace Assignment2.Classes
{
    [Serializable]
    internal class Project
    {
        private string _projectName;
        private int _revenue;
        private string _uuid;

        public Project(string projectName, int revenue)
        {
            _projectName = projectName;
            _revenue = revenue;
            _uuid = General.GenerateUUID();
        }

        public string UUID
        {
            get { return _uuid; }
            set { _uuid = value; }
        }
        
        public string ProjectName
        {
            get { return _projectName; }
            set { _projectName = value; }
        }

        public int Revenue
        {
            get { return _revenue; }
            set { _revenue = value; }
        }

    }
}

This is how I currently add the Project object to the listview

I also created a listView with the name listViewProject that has 3 columns: Name, Revenue, UUID. Is it possible to extend the listViewItem with the Project class such that every time a new Project object is created, it can be directly added to the listView control without having to create a listViewItem similiar to how I can extend a TreeNode class with my own custom class.

Edit: This is how I currently add the project object to the listView

var item = new ListViewItem(new[] {project.UUID,project.ProjectName,project.Revenue.ToString()});

listViewProject.Items.Add(item);

Note: I googled a bit and saw that you can add the Project object as a tag, but thats not what im looking for.

1 Answers

To be able to add a Project straight to ListView with something similar to this:

Project project = new Project
{
    ProjectName = "Big Project",
    Revenue = 12000000
};
listViewProject.Items.Add(project); 

You can use an implicit cast. That would do the trick.

internal class Project
{
    // Automatic, transparent conversion to ListViewItem
    public static implicit operator ListViewItem(Project project)
    {
        return new ListViewItem(new []
        {  
              project.UUID, 
              project.ProjectName, 
              project.Revenue.ToString()
        });
    }
    public string UUID { get; set; } = Guid.NewGuid().ToString();
    public string ProjectName { get; set; }
    public int Revenue { get; set; }
}

TEST

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        listViewProject.Columns.Add(nameof(Project.UUID), 400);
        listViewProject.Columns.Add(nameof(Project.ProjectName), 150);
        listViewProject.Columns.Add(nameof(Project.Revenue), 100);
    }
    private void buttonAddProjectItem_Click(object sender, EventArgs e)
    {
        Project project = new Project
        {
            ProjectName = $"Project {_tstCount++}",
            Revenue = Rando.Next(1000, 10000)
        };
        listViewProject.Items.Add(project);
    }

    char _tstCount = 'A';
    Random Rando = new Random();
}

enter image description here

Related