简体   繁体   中英

Displaying List of Objects on a DataGridView / ListView

I have a List in a class which loads data from a .txt file, which then stores the data in objects located in another class which contains constructors and all. In my form I'm needing to display all the data in a DataGridView/ListView so i can look through it, select and make changes if needed.

However, when I come to do it on the form, nothing works, or it will just display the Constructor names at the top, yet populate no data. I've exhausted all option's, googled left right and center and seems no methods work. Tried things like DataSource = <"class">.theList. Even tried DataBindings etc. but nothing seems to work. It's giving an error like the <"class">.theList cannot be used like a method

Calling upon the great people of Stack OverFlow to assist me in the right direction. Thank you all in advance!

EDIT I have pasted some sample code, any questions regarding it please ask away. Thankyou all.

public class Form{
    private void form_Load()
    {
        Wrapper wrap = new Wrapper();
        List<Child> tempList = new List<Child>();
        tempList = wrap.cList;

        dataGridView1.DataSource = tempList;
    }
}

class Wrapper{
    public List<Child> cList = new List<Child>();

    public void LoadPerson()
    {
        string filePath = @"filepath";
        //StreamReader
        //code to Add each different person + their details into the 
        protected fields
        //stores in cList variables which are in Adult
    }
}

class Adult{
    protected string username, firstName;
    protected int number;
}

class Child: Adult{

    public Child(string Username, string Password, int Number)
    {
        username = Username;
        password = Password;
        number = Number;
    }
    //public getters/setters next
}

Without seeing your code it is difficult to see what the problem could be!

But I would like to suggest another approach if you want to view objects then ObjectListView is an alternative solution.

http://objectlistview.sourceforge.net/cs/index.html

It is a list view and not a datagrid view, but you can simply pass it a collection of your objects and it will show it to you.

It is also very powerful and allows you to customise a lot of things.

Without a code and data sample, it's difficult to give a good advice.

I would create a DataTable out of the data you have, in the following manner (the code needs mod according to the data structure you have constructed):

VB.NET:

Dim dt as New Datatable
dt.Columns.Add("ID", GetType(Integer))
dt.Columns.Add("Name", GetType(String))

For ir = 0 to <SRC>.rows.count -1     ' modify to which ever way you can go through your data
    dt.Rows.Add(<SRC>("ID"), <SRC>("name"))
Next

Me.DataGridView1.datasource = dt

C#:

Datatable dt = new Datatable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));

for (ir = 0; ir <= <SRC>.rows.count - 1; ir++) {
    // modify to which ever way you can go through your data
    dt.Rows.Add(<SRC>("ID"), <SRC>("name"));
}

this.DataGridView1.datasource = dt;

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