【发布时间】:2014-04-07 15:19:45
【问题描述】:
晚上好,
重点:在我的 WPF 应用程序中,我想以在另一个类中创建的 ToString 方法的格式在列表框中显示来自 access 数据库的数据。 -- 我可以显示数据,但它不包含格式。
我的问题背景: 我正在为我在大学的分级单元创建一个应用程序,它可以添加、删除和显示访问数据库中的数据。我在向数据库中添加或删除数据时没有遇到任何问题,但是,我很难以特定格式显示数据。
由于特定要求,我不得不创建一个抽象的Games 类,子类Platform 和Mobile(游戏)。
我想知道如何在列表框中显示来自 access 数据库的数据(尽管这可以灵活更改),同时在 Platform 和 Mobile 中将内容格式化为先前创建的 ToString() 方法班级。我知道我可能必须创建两个单独的方法来显示平台游戏和手机游戏,因为它们都有一个额外的变量。
目前,我将listPlatform() 方法存储在我的Catalogue 类中,该类可从单独的窗口(@987654330@,其中包含列表视图框)访问,然后访问此方法并通过@987654331 调用它@事件。
Catalogue 类 --
public List<string> listPlatform()
{
List<string> data = new List<string>();
string queryString = "SELECT ID, Game_Name, Developer, Publisher, Genre, Age_Rating, Price, Quantity, Description, Platform FROM GameDetails";
using (OleDbConnection connection = new OleDbConnection(ConnString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int id = reader.GetInt32(0);
string gName = reader.GetString(1);
string gDeveloper = reader.GetString(2);
string gPublisher = reader.GetString(3);
string gGenre = reader.GetString(4);
int gAgeRating = reader.GetInt32(5);
var gPrice = reader.GetValue(6);
var gQuantity = reader.GetValue(7);
var gDescription = reader.GetValue(8);
var gPlatform = reader.GetValue(9);
data.Add(id + gName + gDeveloper + gPublisher + gGenre + gAgeRating + gPrice
+ gQuantity + gDescription + gPlatform);
}
reader.Close();
}
return data;
}
EmployeeWindow --
private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
List<string> data = theCatalogue.listPlatform();
lstvwGames.Items.Clear();
foreach (string s in data)
{
lstvwGames.Items.Add(s);
}
}
Platform 类 --
/// <summary>
/// Returns a string representation of a Platform game
/// </summary>
/// <returns></returns>
public override string ToString()
{
string strout = string.Format(base.ToString() + "Platform:{0}", platform);
return strout;
}
我希望我的问题是有道理的,并且我已经提供了足够的信息,让您对我正在尝试做的事情有所了解。
【问题讨论】:
-
您能否解释一下您希望/希望看到的列表框项目是什么?
-
类似以下内容:ID:名称:出版商:开发人员:类型:年龄评级:等等。我已将其编码为我的抽象
ToString()类中的基本ToString()方法。 -
请注意标签是独立的。也就是说,您不能组合多个标签来创建一个概念。标签
[access]和[database]一起与单个[ms-access]标签不同。请务必阅读选择标签时出现的说明! -
谢谢,下次一定要小心点。