Search code examples
c#listviewcheckboxcheckboxlistobjectlistview

ObjectlistView Checkbox Issue


I have an issue trying to get a Checkbox working with ObjectListview.

My model looks like this:

public class object
{   
    public string name {get; set;}
    public int age {get; set;}
    public bool inuse {get; set;}
}

And I added a FastObjectListView via the Designer in Visual Studio to a Win Forms Application. Then, I added the Columns and set the AspectName for each column to the Models Property (First column: AspectName: name, Second Column: AspectName: age, Third Column: AspectName: inuse).

Afterwards, I filled the ListView with this:

using (var context = new objectDb())
{
    var objectlist = context.objects.ToList();
    fastoLV_Clean.SetObjects(objectlist);                             
    fastoLV_Clean.Refresh();
}       

That works, and I can see my Database entries in the ListView.

Now I want to add a CheckBox column where someone can check or uncheck the items to delete them and I can not get the checkbox to work.

I have added a Column and set CheckBox to true, changed the CheckedAspectName of the ListView and now I can see the Checkboxes but nothing happens if I click them to check. I think I'm on the completely wrong track, what do I have to do to make it work?

Thank you very much!!


Solution

  • I don't know a way with the ObjectListView to include any items which are not part of your model.

    So then the simple way is to change your model to include a "Delete" property which you can then show in your ObjectListView.

    Of course, this is not always possible! Especially if you are dealing with items that are written to/from Database or another persistence layer.

    Then the trick is to write a derived class with you model being the base class and then you just add the delete column to this. But then you would need to convert from your Base to a derived class before showing in the ObjectListView.

    The following code can help with that.

    You keep your columns set-up as you have already done. Assuming your (now base) class is defined like this

     public class MyClass
     {   
         public string name { get; set; }
         public int age { get; set; }
         public bool inuse { get; set; }
     }
    

    Your derived class inherits from this, adds the delete property and a new constructor

    public class MySecondClass : MyClass
    {
        public bool Delete { get; set; }
    
        public MySecondClass(MyClass other)
        {
            //Copy from MyClass
            this.name = other.name;
            this.age = other.age;
            this.inuse = other.inuse;
    
            //Set default for new properties
            this.Delete = false;
        }
    }
    

    Your code to retrieve the objects and set them then looks like this

    using (var context = new objectDb())
    {
        var objectlist = context.objects.ToList();
    
        //Now we need to convert to the derived class type
         var secondlist = list.ConvertAll(x => new MySecondClass(x));
    
         //Then we setobjects using this new list
         fastoLV_Clean.SetObjects(secondlist);     
    }