开发者

c# Display the data in the List view

开发者 https://www.devze.com 2022-12-28 14:13 出处:网络
private void displayResultsButton_Click(object sender, EventArgs e) { gameResultsListView.Items.Clear();
private void displayResultsButton_Click(object sender, EventArgs e)
    {
        gameResultsListView.Items.Clear();

        //foreach (Game game in footballLeagueDatabase.games)
        //{
            ListViewItem row = new ListViewItem();
            row.SubItems.Add(game.HomeTeam.ToString());
            row.SubItems.Add(game.HomeScore.ToString());
            row.SubItems.Add(game.AwayTeam.ToString());
            row.SubItems.Add(game.AwayScore.ToString());
            gameResultsListView.Items.Add(row);
       // }
        //footballLeagueDatabase.games.Sort();
    }
   }
}

This is the display button and the following code describes the add button.

pri开发者_运维技巧vate void addGameButton_Click(object sender, EventArgs e)
    {
        if ((homeTeamTxt.Text.Length) == 0)
            MessageBox.Show("You must enter a Home Team");
        else if (homeScoreUpDown.Maximum <= 9 && homeScoreUpDown.Minimum >= 0)
            MessageBox.Show("You must enter one digit between 0 and 9");
        else if ((awayTeamTxt.Text.Length) == 0)
            MessageBox.Show("You must enter a Away Team");
        else if (awayScoreUpDown.Maximum <= 9 && awayScoreUpDown.Minimum >= 0)
            MessageBox.Show("You must enter one digit between 0 to 9");
        else 
        {
            //checkGameInputFields();
            game = new Game(homeTeamTxt.Text, int.Parse(homeScoreUpDown.Value.ToString()), awayTeamTxt.Text, int.Parse(awayScoreUpDown.Value.ToString()));
            MessageBox.Show("Home Team" + 't' + homeTeamTxt.Text + "Away Team" + awayTeamTxt.Text + "created");
            footballLeagueDatabase.AddGame(game);

            //clearCreateStudentInputFields();
        }
    }

I need to insert data into the above text field and Numeric up down control and display them in the list view. But I dont know How to do it, because when I press the button "Display Results" it displays the error message.

If you know how can I display the data in the list view, please let me know.This is the first time I am using List view.


Based on the error message that you specified, I believe that your problem is that the game object that you use to create the subitems is not instantiated. I noticed that you commented out the foreach that created this object. So, make sure that you create the object called game.

In addition to this you didn't specify how you wanted the data to appear. If you want all subitems to appear in one row, then you also need to add columns - unless you have already done this using the designer that is. Otherwise your code needs to be something like this:

private void displayResultsButton_Click(object sender, EventArgs e) {
    Game game = new Game("Home", 10, "Away", 12);

    gameResultsListView.Items.Clear();
    //You need this so that the subitems are also displayed, otherwise
    //only the main item will show in the listview
    gameResultsListView.View = View.Details;

    ListViewItem row = new ListViewItem("Game 1");
    row.SubItems.Add(game.HomeTeam.ToString());
    row.SubItems.Add(game.HomeScore.ToString());
    row.SubItems.Add(game.AwayTeam.ToString());
    row.SubItems.Add(game.AwayScore.ToString());

    //The main item and each subitem get a column in the listView
    gameResultsListView.Columns.Add("Game", -2, HorizontalAlignment.Left);
    gameResultsListView.Columns.Add("HomeTeam", -2, HorizontalAlignment.Left);
    gameResultsListView.Columns.Add("HomeTeamScore", -2, HorizontalAlignment.Left);
    gameResultsListView.Columns.Add("AwayTeam", -2, HorizontalAlignment.Left);
    gameResultsListView.Columns.Add("AwayTeamScore", -2, HorizontalAlignment.Left);

    gameResultsListView.Items.Add(row);
}

Notice how I am creating a Game object at the top. If the above code works, then the problem was with the game object not being instantiated in your original code. If it works you at least know that the problem is not within this function. Also, are you testing the application from within the IDE, because if you are you should have been getting more messages than just the one you showed and it would take you to the line that is causing problems.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号