在 DataGridView / ListView 上显示对象列表
Displaying List of Objects on a DataGridView / ListView
我在 class 中有一个列表,它从 .txt 文件加载数据,然后将数据存储在位于另一个 class 的对象中,其中包含构造函数和所有内容。在我的表单中,我需要在 DataGridView/ListView 中显示所有数据,以便我可以查看它,select 并在需要时进行更改。
但是,当我在表单上执行此操作时,没有任何效果,或者它只会在顶部显示构造函数名称,但不会填充任何数据。我用尽了所有选项,用谷歌左右搜索,似乎没有任何方法有效。尝试过 DataSource = <"class">.theList 之类的东西。甚至尝试过 DataBindings 等,但似乎没有任何效果。它给出了一个错误,如 the <"class">.theList cannot be used like a method
呼吁 Stack OverFlow 的伟大人士在正确的方向上帮助我。提前谢谢大家!
编辑
我已经粘贴了一些示例代码,有关它的任何问题请提出。谢谢大家
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
}
如果不查看您的代码,就很难看出问题所在!
但我想建议另一种方法,如果您想查看对象,那么 ObjectListView 是另一种解决方案。
http://objectlistview.sourceforge.net/cs/index.html
它是一个列表视图而不是数据网格视图,但您可以简单地将对象的集合传递给它,它会向您显示。
它也很强大,可以自定义很多东西。
没有代码和数据示例,很难给出好的建议。
我将按照以下方式从您拥有的数据中创建一个 DataTable(根据您构建的数据结构,代码需要 mod):
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;
我在 class 中有一个列表,它从 .txt 文件加载数据,然后将数据存储在位于另一个 class 的对象中,其中包含构造函数和所有内容。在我的表单中,我需要在 DataGridView/ListView 中显示所有数据,以便我可以查看它,select 并在需要时进行更改。
但是,当我在表单上执行此操作时,没有任何效果,或者它只会在顶部显示构造函数名称,但不会填充任何数据。我用尽了所有选项,用谷歌左右搜索,似乎没有任何方法有效。尝试过 DataSource = <"class">.theList 之类的东西。甚至尝试过 DataBindings 等,但似乎没有任何效果。它给出了一个错误,如 the <"class">.theList cannot be used like a method
呼吁 Stack OverFlow 的伟大人士在正确的方向上帮助我。提前谢谢大家!
编辑 我已经粘贴了一些示例代码,有关它的任何问题请提出。谢谢大家
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
}
如果不查看您的代码,就很难看出问题所在!
但我想建议另一种方法,如果您想查看对象,那么 ObjectListView 是另一种解决方案。
http://objectlistview.sourceforge.net/cs/index.html
它是一个列表视图而不是数据网格视图,但您可以简单地将对象的集合传递给它,它会向您显示。
它也很强大,可以自定义很多东西。
没有代码和数据示例,很难给出好的建议。
我将按照以下方式从您拥有的数据中创建一个 DataTable(根据您构建的数据结构,代码需要 mod):
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;