Home > other >  C# extend a listView containing columns with custom object
C# extend a listView containing columns with custom object

Time:07-23

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.

CodePudding user response:

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 enter image description here

  • Related