用类中的数据填充ListBox. [英] Populate ListBox with data from a class.
问题描述
你好,
我正在尝试从我从数据库中检索到的类的一些数据获取表单中的列表框.我不知道该怎么做.我尝试搜索Google和其他网站而没有成功.以下是我的代码.
Hello,
I am trying to get some data from a class that I retrievied from my database into a listbox in the form. I have no idea how to do this. I tried searching google and other sites without succes. Below is my code.
// Class: BugFinder
public void GetBugs()
{
MySqlDataReader reader = Database.Reader("select * from bugs");
while (reader.Read())
{
Bug bug = new Bug();
bug.id = reader.GetInt32("id");
bug.name = reader.GetString("name");
bug.project = reader.GetString("project");
bug.category = reader.GetString("category");
bug.viewStatus = reader.GetString("viewstatus");
bug.reporter = reader.GetString("reporter");
bug.assignedTo = reader.GetString("assignedto");
bug.priority = reader.GetString("priority");
bug.severity = reader.GetString("severity");
bug.reproducibillity = reader.GetString("reproducibillity");
bug.status = reader.GetString("status");
bug.platform = reader.GetString("platform");
bug.os = reader.GetString("os");
bug.osVersion = reader.GetString("osversion");
bug.summary = reader.GetString("summary");
bug.description = reader.GetString("description");
bug.reproduceSteps = reader.GetString("reproducesteps");
bug.addInfo = reader.GetString("addinfo");
bug.dateSub = reader.GetDateTime("datesub");
bug.lastUpdate = reader.GetDateTime("lastupdate");
}
}
// Class: Bug
public int id;
public string name;
public string project;
public string category;
public string viewStatus;
public string reporter;
public string assignedTo;
public string priority;
public string severity;
public string reproducibillity;
public string status;
public string platform;
public string os;
public string osVersion;
public string summary;
public string description;
public string reproduceSteps;
public string addInfo;
public DateTime dateSub;
public DateTime lastUpdate;
// Main form:
// Listbox name: bugsBox/
我不知道如何将错误放入列表框. (我知道如何填充列表框,列表视图.但不是这种方式.)
在此先感谢您.
I have no idea how to get the bugs into a listbox. ( I know how to populate a listbox, listview. But not in this way.)
Thanks in advance.
推荐答案
您需要做的第一件事是保留一个列表或类似的错误,这些错误是您从数据库中读取它们时创建的.否则,您将无法将它们添加到任何内容中.
我建议在课堂上使用List< T> ;:
The first thing you need is to keep a list or similar of the bugs you are creating as you read them from the DB. Otherwise you cannot add them to anything!
I would suggest at class level you use a List<T>:
// Class: BugFinder
private List<Bug> myBugs;
public void GetBugs()
{
MySqlDataReader reader = Database.Reader("select * from bugs");
myBugs = new List<Bug>();
while (reader.Read())
{
Bug bug = new Bug();
bug.id = reader.GetInt32("id");
...
bug.lastUpdate = reader.GetDateTime("lastupdate");
myBugs.Add(bug);
}
}
将错误添加到列表框中的操作取决于要在列表框中显示的内容.您说您知道如何填充ListBox-是什么阻止您添加Bug信息?
好,但是现在我可以将列表添加到我的列表框/列表视图中.像这样:
What you have to do to add the bugs to a List box depends on what you want to display in the list box. You say you know how to populate a ListBox - what is stopping you from adding your Bug information?
Ok, but now I can just add the list to my listbox/listview. Like this:
BugFinder finder = new BugFinder();
finder.GetBugs();
recentlyBox.Items.Add(""); (What should I put here?)
这取决于您认为需要显示的内容.我很想去做一些小的改动:
1)让您的GetBugs方法返回错误列表:
It depends on what you think you need to display. I would be tempted to go for a couple of small changes:
1) Make your GetBugs method return the list of bugs:
public List<Bugs> GetBugs()
{
MySqlDataReader reader = Database.Reader("select * from bugs");
myBugs = new List<Bug>();
while (reader.Read())
{
Bug bug = new Bug();
bug.id = reader.GetInt32("id");
...
bug.lastUpdate = reader.GetDateTime("lastupdate");
myBugs.Add(bug);
}
return myBugs;
}
然后,您可以直接使用它们.
2)使Bug作为ToString方法返回用户可能想要阅读的内容:
You can then use them directly.
2) Make Bugs return something the user might want to read as the ToString method:
public override string ToString()
{
return name; // just an idea, you may want more here!
}
然后可以添加您的错误:
You can then add your bugs:
BugFinder finder = new BugFinder();
foreach (Bug bug in finder.GetBugs())
{
recentlyBox.Items.Add(bug.ToString());
}
这篇关于用类中的数据填充ListBox.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!