在文本框中显示列表

时间:2013-04-09 11:56:30

标签: c# list

我正在尝试创建一个对象列表来创建一个联赛表。

以下是为class创建的object

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多个次。

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

5 个答案:

答案 0 :(得分:5)

您将object添加到文字中,当您在对象上调用ToString()时,您将获得类型/命名空间,我怀疑您需要该属性。您需要执行以下操作: -

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

显然用正确的属性替换teamName

答案 1 :(得分:2)

覆盖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);
        }
    }

答案 2 :(得分: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);
}

答案 3 :(得分:0)

你这样做很好,但是,你需要覆盖你班级中的ToString方法,并在那里定义你希望字符串看起来像什么。

答案 4 :(得分:0)

检查您正在使用的数据阅读器,如果数据阅读器中有多行,则必须遍历它以选择正确的值。像

这样的东西
    dr[i]["teamName"];