2017-10-10 152 views
-1

我在一个类中加载了一个List,它从.txt文件加载数据,然后将数据存储在另一个包含构造函数和其他类的对象中。在我的表单中,我需要在DataGridView/ListView中显示所有数据,以便我可以查看它,如果需要,请选择并进行更改。在DataGridView/ListView上显示对象列表

但是,当我在表单上完成它时,没有任何工作,或者它只会在顶部显示构造函数名称,但不会填充任何数据。我用尽了所有选项,搜索了左右和中心,似乎没有任何方法可行。试过像DataSource = <“class”>。theList。即使尝试过DataBindings等,但似乎没有任何工作。它给人一种错误,如的<“类”>的thelist不能等的方法

在堆栈溢出的大人们呼唤,以帮助我在正确的方向上使用。谢谢大家!

编辑 我已经粘贴了一些示例代码,有关它的任何问题请求离开。谢谢你们。

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 
} 
+0

请帮助我们提供代码,这些代码是您迄今为止尝试使用以获得更多理解的代码。 – mmushtaq

+0

已编辑代码 – afx31

+0

'DataGridView.DataSource = listOfChilds;' - 应该工作。 'Child'类是否具有您想要在datagridview中查看/编辑的字段的属性? – Fabio

回答

0

没有代码和数据样本,很难给出一个好建议。

VB.NET:

我会创建一个DataTable出你有数据的,以下面的方式(该代码根据已构造的数据结构需要MOD)

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; 
+0

已经粘贴了一些代码 – afx31

+0

而这段代码粘贴在你的问题中的作品?测试? “公共Chil = ...”中有一个小错字,所以它无法工作。无论如何,你在调试时看到“templist”列表变量中的预期数据吗?如果是的话,你应该没问题,用上面提供的代码更改templist ,它应该创建一个有效的DataTable并填充DataGridView。 –

+0

那时我只是输入一个错字,而忽略了那个抱歉。 嗯,我只是试着用foreach循环检查tempList中的每个孩子,它只是跳过循环,所以假设什么都没有.. – afx31

0

没有看到您的代码,很难看出问题是什么!

但我想建议另一种方法,如果你想查看对象然后ObjectListView是一个替代解决方案。

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

这是一个列表视图,而不是数据网格视图,但你完全可以通过它您的对象的集合,它会拿出来给你。

它也非常强大,可以让你自定义很多东西。

+0

已经粘贴了一些代码 – afx31