简体   繁体   中英

C# DataGrid linking to List of Objects with properties

I have a datagridview and a list of objects... I am trying to set the datasource of the grid to this list, but somehow the datagridview always turns up empty. Although, when I debug, the datasource property of the datagridview is populated by a list of my objects

Here is what I got how my object looks like:

 public class TextIFileObject
    {
        public string name;
        public long totalChars;
        public bool bearbeitet;
        public string path; ....}

I populate the list like this:

    public BindingList<TextIFileObject> allDetectedFiles = new BindingList<TextIFileObject>();
FileInfo[] Files = dinfo.GetFiles("*.txt");
                foreach (FileInfo file in Files)
                {
                    TextIFileObject tmp = new TextIFileObject(file);
                    allDetectedFiles.Add(tmp);

                }

and finally I set the source of the datagrid like this:

 dataGridView1.AutoGenerateColumns = true;
            dataGridView1.AutoSize = true;
            dataGridView1.DataSource = allDetectedFiles;

I have draged the datagridview out on the form from the toolbox (if somehow that is relevant!)

and here is how my form looks like: (the datagrid is simply empty! )

在此处输入图片说明

I wrote a small piece of Code to show it via example:

https://github.com/kelrien/DotNetExampleDataBinding

The problem was I was not using publicly visible getters!
something like this:

public class TextIFileObject
    {
        public string name { get; set; }
        public long totalChars{ get; set; }
        public bool bearbeitet{ get; set; }
        public string path; ....}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM