【问题标题】:Formatting and displaying data from an access database (WPF, C#)格式化和显示来自访问数据库(WPF、C#)的数据
【发布时间】:2014-04-07 15:19:45
【问题描述】:

晚上好,

重点:在我的 WPF 应用程序中,我想以在另一个类中创建的 ToString 方法的格式在列表框中显示来自 access 数据库的数据。 -- 我可以显示数据,但它不包含格式。

我的问题背景: 我正在为我在大学的分级单元创建一个应用程序,它可以添加、删除和显示访问数据库中的数据。我在向数据库中添加或删除数据时没有遇到任何问题,但是,我很难以特定格式显示数据。

由于特定要求,我不得不创建一个抽象的Games 类,子类PlatformMobile(游戏)。

我想知道如何在列表框中显示来自 access 数据库的数据(尽管这可以灵活更改),同时在 PlatformMobile 中将内容格式化为先前创建的 ToString() 方法班级。我知道我可能必须创建两个单独的方法来显示平台游戏和手机游戏,因为它们都有一个额外的变量。

目前,我将listPlatform() 方法存储在我的Catalogue 类中,该类可从单独的窗口(@98​​7654330@,其中包含列表视图框)访问,然后访问此方法并通过@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] 标签不同。请务必阅读选择标签时出现的说明!
  • 谢谢,下次一定要小心点。

标签: c# wpf ms-access


【解决方案1】:

我认为,在这种情况下,您需要在代码中添加另一个类。
Game class,您可以通过查看数据库表中存在的字段来建模

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 类中,当您读取数据库时,您构建的是 List&lt;Game&gt; 而不是 List&lt;String&gt;

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;
}

最后,当您需要将游戏添加到您的 ListBox 时,您可以编写

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());
    }         
}

你有一个列表,里面有 GameName 和 Genre。

编辑以完成此答案

最后,您可以使用您的List&lt;Game&gt; 直接设置 ListBox 的DataSourceDisplayMemberValueMember 属性,并且它的一些属性无需循环来填充项目

private void btnDisplay_Click(object sender, RoutedEventArgs e)
{
    lstvwGames.ValueMember = "ID";
    lstvwGames.DisplayMember = "GameData";
    lstvwGames.DataSource = theCatalogue.listPlatform();
}

在此示例中,DisplayMember 属性被分配给您应该在 Game 类中定义的新 GameData 字段。这个新的只读属性可以是同一类的 ToString 方法的实际返回值,也可以是您选择的其他值

public class Game
{
    .....
    public string GameData 
    {
        // Only a getter, thus readonly
        get
        {
            return this.ToString();
        }
    }
}

当然,您可以更改 Game 类中的 ToString 方法或 GameData 属性,以返回您真正希望在列表框中显示的信息。

【讨论】:

  • 谢谢,非常感谢。
【解决方案2】:

我没有看到您使用 tostring 覆盖方法。也许您打算在这里使用它?

lstvwGames.Items.Add(s.ToString());

【讨论】:

  • 感谢回复,我试过了,没有任何变化。
猜你喜欢
  • 1970-01-01
  • 2017-07-03
  • 2019-04-14
  • 1970-01-01
  • 2013-10-27
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
相关资源
最近更新 更多