2

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.

3
  • I guess it's possible, but not in a pretty or practical way. You could create a function or read-only property in your Project class that returns its ListViewItem representation; but perhaps your best bet would be to use a third-party bindable list view (like this one) and bind it to a collection of Projects Commented Jul 21, 2022 at 17:01
  • 1
    It is difficult to follow what you are asking. Are you stuck using a ListView? You could use a DataGridView, then use a BindingList of Project objects … BindingList<Project> … as a DataSouce for the grid. Then you could simply add new Project objects to the BindingList<Project> and it will automatically update/add the new Project object to the grid. I am guessing this what you are asking. Commented Jul 21, 2022 at 18:36
  • Create an extension method Add for the ListView.ListViewItemCollection type that takes a Project object where you use it to create a new ListViewItem and add it to the collection? Commented Jul 21, 2022 at 18:37

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.