Greg
Greg

Reputation: 34818

which winforms control/approach to bind to a List<> collection of custom objects?

Which control approach can i use to quickly provide visual editing of my List collection.
The in-memory collection I have is below.

My requirements are basically to:

  1. provide a means on my winform form, to allow add/view/edit of the List of ConfigFileDTO information, BUT
  2. Only the "PATH" field of the ConfigFileDTO needs to be made a available to the user, therefore the use could:
    • add a new PATH to the list,
    • delete PATHs, hence deleting the ConfigFileDTO from the list,
    • and edit the list, allowing one of the PATH's in the list to be changed.

My code

    private static List<ConfigFileDTO> files;

    public class ConfigFileDTO
    {
        private string filename, content_type, path;
        private int file_size;
        private DateTime updated_at;

        public ConfigFileDTO() {  }

        public int FileSize {
            get { return this.file_size;  }
            set { this.file_size = value; }
        }    
        public string ContentType {
            get { return this.content_type; }
            set { this.content_type = value; }
        }    
        public string Filename {
            get { return this.filename; }
            set { this.filename = value; }
        }    
        public DateTime UpdatedAt {
            get { return this.updated_at; }
            set { this.updated_at = value; }
        }    
        public string Path {
            get { return this.path; }
            set { this.path = value; }
        }    
    }

Thanks

Upvotes: 2

Views: 392

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063649

If you only want the Path column to be manipulated, then it is usually better to simply set up the column bindings (for things like DataGridView) manually; however, you can also use things like [Browsable(false)] (removes a property from display) and [ReadOnly(true)] (treat a property as read-only even if it has a setter) to control how properties (/columns) are handled.

If you want to control how new instances are created, inherit from BindingList<T> and override AddNewCore().

Upvotes: 2

Related Questions