JackofAll
JackofAll

Reputation: 537

Displaying a List in Textbox

I am trying to create a list of object to create a league table.

Here is the class created for the 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; }

}

I am then trying to add each row, and display each row in literal1 to see the contents of the list. Seen here:

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

I am having an issue as the correct number of rows are displaying in the literal, however the data just has the name of the project and the row name like: PROJECTNAME.LeagueRow multiple times.

Can anyone tell me what I am doing wrong?

Upvotes: 0

Views: 5027

Answers (5)

Harish
Harish

Reputation: 1519

Check the data reader you are using, if you have bunch of rows in the data reader, you must loop through it to pick the right value. Something like

    dr[i]["teamName"];

Upvotes: 0

John Willemse
John Willemse

Reputation: 6698

The way you are doing it is just fine, however, you need to override the ToString method in your class and define there what you want the string to look like.

Upvotes: 0

Nicolas Tyler
Nicolas Tyler

Reputation: 10552

The LeagueRow.ToString() method is the base method from Object.

You either need to overide the ToString() method or concatenate the strings you want to display.

Overide:

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

concatenate:

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

Upvotes: 0

laszlokiss88
laszlokiss88

Reputation: 4071

Override the ToString method of your LeagueRow class to return the right thing to display (for example, the team and managername).

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

Upvotes: 3

DGibbs
DGibbs

Reputation: 14608

You're adding the object to the literal, when you call ToString() on an object you will just get the type/namespace, i suspect you want the property. You need to do something like: -

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

Obviously replacing teamName with the correct property.

Upvotes: 4

Related Questions