格式化和显示access数据库(WPF,C#)中的数据
晚上好 切中要害:在我的WPF应用程序中,我希望以另一个类中创建的格式化和显示access数据库(WPF,C#)中的数据,c#,wpf,ms-access,C#,Wpf,Ms Access,晚上好 切中要害:在我的WPF应用程序中,我希望以另一个类中创建的ToString方法的格式在列表框中显示access数据库中的数据我可以显示数据,但它不包含格式 我问题的背景: 我正在为我在大学的分级单元创建一个应用程序,它可以添加、删除和显示access数据库中的数据。我在向数据库添加或删除数据方面没有问题,但是,我很难以特定格式显示数据 由于特定的需求,我不得不创建一个抽象的Games类,其中包含Platform和Mobile(游戏)的子类 我想知道如何在列表框中显示access数据库中的
ToString
方法的格式在列表框中显示access数据库中的数据我可以显示数据,但它不包含格式
我问题的背景:
我正在为我在大学的分级单元创建一个应用程序,它可以添加、删除和显示access数据库中的数据。我在向数据库添加或删除数据方面没有问题,但是,我很难以特定格式显示数据
由于特定的需求,我不得不创建一个抽象的Games
类,其中包含Platform
和Mobile
(游戏)的子类
我想知道如何在列表框中显示access数据库中的数据(尽管这可以灵活更改),同时将内容格式化为以前在平台
和移动
类中创建的ToString()
方法。我知道我可能需要创建两个单独的方法来显示平台和手机游戏,因为它们都有一个额外的变量
目前,我正在将我的listPlatform()
方法存储在我的Catalog
类中,该类可从单独的窗口(EmployeeWindow
)访问,该窗口包含列表视图框,然后访问此方法并通过按钮单击事件调用它
目录
类--
平台
类--
//
///返回平台游戏的字符串表示形式
///
///
公共重写字符串ToString()
{
string strout=string.Format(base.ToString()+“平台:{0}”,平台);
回程行程;
}
我希望我的问题是有意义的,并且我已经提供了足够的信息,让您了解我正在尝试做什么。我没有看到您使用tostring override方法。也许您打算在这里使用它
lstvwGames.Items.Add(s.ToString());
我认为,在这种情况下,您需要向代码中添加另一个类。
查看数据库表中的字段,可以对其建模的游戏类
public class Game
{
public int ID {get;set;}
public string GameName {get;set;}
public string Developer {get;set;}
public string Publisher {get;set;}
public string Genre {get;set;}
public string Age_Rating {get;set;}
public decimal Price {get;set;}
public int Quantity {get;set;}
public string Description {get;set;}
public string Platform {get;set;}
public override string ToString()
{
return this.GameName + " - " + this.Genre;
}
}
现在在Catalog类中,当您阅读数据库时,您将构建一个列表
而不是列表
在本例中,DisplayMember
属性被分配给一个新的GameData
字段,您应该在游戏类中定义该字段。这个新的只读属性可以是同一类的ToString方法的实际返回值,也可以是您选择的另一个值
public class Game
{
.....
public string GameData
{
// Only a getter, thus readonly
get
{
return this.ToString();
}
}
}
当然,您可以更改Game类中的ToString方法或GameData属性,以返回您真正希望在列表框中显示的信息。感谢您的回复,我已经尝试过了,没有任何更改。您能解释一下您希望/希望看到的列表框项目是什么吗?类似于:ID:Name:Publis的内容她:开发者:流派:年龄等级:等等。我在我的抽象游戏类中将此编码为基本ToString()
方法。请注意,标记是独立的。也就是说,您不能将多个标记组合成一个概念。标记[access]
和[database]
一起使用与单个[ms access]
标签不同。请务必阅读选择标签时出现的说明!谢谢,我会确保下次更加小心。谢谢,非常感谢。
lstvwGames.Items.Add(s.ToString());
public class Game
{
public int ID {get;set;}
public string GameName {get;set;}
public string Developer {get;set;}
public string Publisher {get;set;}
public string Genre {get;set;}
public string Age_Rating {get;set;}
public decimal Price {get;set;}
public int Quantity {get;set;}
public string Description {get;set;}
public string Platform {get;set;}
public override string ToString()
{
return this.GameName + " - " + this.Genre;
}
}
public List<Game> listPlatform()
{
.....
List<Game> games = new List<Game>();
while (reader.Read())
{
Game g = new Game();
g.ID = reader.GetInt32(0);
g.GameName = reader.GetString(1);
... and so on for the rest of fields
games.Add(g);
}
...
return games;
}
private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
List<Game> data = theCatalogue.listPlatform();
lstvwGames.Items.Clear();
foreach (Game g in data)
{
lstvwGames.Items.Add(g.ToString());
}
}
private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
lstvwGames.ValueMember = "ID";
lstvwGames.DisplayMember = "GameData";
lstvwGames.DataSource = theCatalogue.listPlatform();
}
public class Game
{
.....
public string GameData
{
// Only a getter, thus readonly
get
{
return this.ToString();
}
}
}