0

我正在尝试创建一个对象列表来创建一个排行榜。

这是classobject

public class LeagueRow
{
    public String teamName { get; set; }
    public String managerName { get; set; }
    public Int32 played { get; set; }
    public Int32 won { get; set; }
    public Int32 draw { get; set; }
    public Int32 lost { get; set; }
    public Int32 goalsFor { get; set; }
    public Int32 goalsAgainst { get; set; }
    public Int32 goalDifference { get; set; }
    public Int32 points { get; set; }

}

然后我尝试添加每一行,并显示每一行literal1以查看列表的内容。在这里看到:

goalDifference = goalsFor - goalsAgainst;

List<LeagueRow> leagueTeamRow = new List<LeagueRow>();
leagueTeamRow.Add(new LeagueRow { teamName = dReader["teamName"].ToString(), managerName = dReader["ManagerFirstName"].ToString() + " " + dReader["ManagerSurname"].ToString(), played = gamesPlayed, won = won, draw = draw, lost = lost, goalsFor = goalsFor, goalsAgainst = goalsAgainst, goalDifference = goalDifference, points = points });

for (int i = 0; i < leagueTeamRow.Count; i++)
{
    Literal1.Text += (leagueTeamRow[i].ToString());
}

我遇到了一个问题,因为在rows中显示了正确的数量literal,但是数据只有项目的名称和行名称,例如:PROJECTNAME.LeagueRow多次。

谁能告诉我我做错了什么?

4

5 回答 5

5

您正在向object文字添加 ,当您调用ToString()一个对象时,您只会获得类型/命名空间,我怀疑您想要该属性。您需要执行以下操作:-

Literal1.Text += (leagueTeamRow[i].teamName);

显然替换teamName为正确的属性。

于 2013-04-09T11:58:36.927 回答
3

覆盖LeagueRow类的 ToString 方法以返回要显示的正确内容(例如,团队和经理名称)。

 public class LeagueRow
    {
        public String teamName { get; set; }
        public String managerName { get; set; }
        public Int32 played { get; set; }
        public Int32 won { get; set; }
        public Int32 draw { get; set; }
        public Int32 lost { get; set; }
        public Int32 goalsFor { get; set; }
        public Int32 goalsAgainst { get; set; }
        public Int32 goalDifference { get; set; }
        public Int32 points { get; set; }

        public override string ToString()
        {
            return string.Format("TeamName: {0}, ManagerName: {1}", teamName, managerName);
        }
    }
于 2013-04-09T11:59:58.320 回答
0

LeagueRow.ToString()方法是来自 的基础方法Object

您需要覆盖该ToString()方法或连接要显示的字符串。

覆盖:

public class LeagueRow
{
    public String teamName { get; set; }
    public String managerName { get; set; }
    public Int32 played { get; set; }
    public Int32 won { get; set; }
    public Int32 draw { get; set; }
    public Int32 lost { get; set; }
    public Int32 goalsFor { get; set; }
    public Int32 goalsAgainst { get; set; }
    public Int32 goalDifference { get; set; }
    public Int32 points { get; set; }

    public override string ToString()
    {
        return string.Format("TeamName: {0}, ManagerName: {1}", teamName, managerName);
    }
}

连接:

goalDifference = goalsFor - goalsAgainst;

List<LeagueRow> leagueTeamRow = new List<LeagueRow>();
leagueTeamRow.Add(new LeagueRow { teamName = dReader["teamName"].ToString(), managerName = dReader["ManagerFirstName"].ToString() + " " + dReader["ManagerSurname"].ToString(), played = gamesPlayed, won = won, draw = draw, lost = lost, goalsFor = goalsFor, goalsAgainst = goalsAgainst, goalDifference = goalDifference, points = points });

for (int i = 0; i < leagueTeamRow.Count; i++)
{
    Literal1.Text += (leagueTeamRow[i].teamName+"\t"+leagueTeamRow[i].managerName);
}
于 2013-04-09T12:00:29.450 回答
0

您这样做的方式很好,但是,您需要覆盖ToString类中的方法并在那里定义您希望字符串的外观。

于 2013-04-09T12:00:59.387 回答
0

检查您正在使用的数据读取器,如果数据读取器中有一堆行,则必须循环遍历它以选择正确的值。就像是

    dr[i]["teamName"];
于 2013-04-09T12:01:58.470 回答