Home > Blockchain >  Array of variables to DataGridView table in Windows forms?
Array of variables to DataGridView table in Windows forms?

Time:01-01

So I have a class of public variables for use in a scoring system, but can't seem to find any easy way to print these all to different columns in a table.

The code for that class is below, you can see it's just a load of public arrays.

    {
        public static string[] playerName = new string[9];
        public static int[] playerTotalScore = new int[9];
        public static int[] playerEventScore = new int[9];
    }

and i want it to be output as a table, the first column showing the Names, the second column showing the total scores. How can I make it do this? As i can't seem to find any solution for this that fits my use case

CodePudding user response:

Try this way:

public class Player
{
    public int Id { get; set; }
    public string Name { get; set; }
    public long TotalScore { get; set; }
    public long EventScore { get; set; }
}

For Save:

Player player= new Player();
player.Name="player_name";
player.TotalScore =1200;
player.EventScore =123;

List<Player> players= new List<Player>();
players.add(player);

CodePudding user response:

you can see it's just a load of public arrays.

In common with masoud's answer, I want to point out that we don't do this in OO languages:

//data store for 10 people
public string[] Names = new string[10];
public int[] Scores = new int[10];
public DateTime[] Birthdays = new DateTime[10];

We don't declare 10 arrays for the names, 10 for the score, 10 for the birthdays etc, and then have a "the data in the same position is from the same person:

Names[0] = "John";
Scores[0] = 99;
Birthdays[0] = new DateTime(1970,1,1);

When we want to keep data together, we make a class that represents it:

public class Person{
  public string Name {get;set;}
  public int Score {get;set;}
  public DateTime Birthday {get;set;}
}

Then we can have an array of 10 of those if you want...

var people = new Person[10];

By the way, in modern C# we can use records to quickly declare classes that hold data. They also have some other features that make them useful for things like being stored in dictionaries and compared to each other:

public record Person(string Name, int Score, DateTime Birthday) //roughly the same as above

So now you're down with this concept, you can populate a DataGridView quickly and easily.. Quick intro to DataTables:

A DataTable is a collection of DataRow. It also has a collection of DataColumn. Controls like DataGridview will use the DataColumns to know how many columns to draw, as well as using the rows. The DataTable is like the Person[10] above, and each DataRow is like an individual Person

var dt = new DataTable();
dt.Columns.Add("Name"); //string, by default
dt.Columns.Add("Score", typeof(int));
dt.Columns.Add("Birthday", typeof(DateTime));

Then you fill the rows with data:

dt.Rows.Add("John", 90, new DateTime(1970,1,1));
dt.Rows.Add("Sarah", 78, new DateTime(1970,1,2));
dt.Rows.Add("Jane", 99, new DateTime(1970,1,3));

Then you tell the DataGridView to use it as a source of data:

peopleDataGridView.DataSource = dt;

A DataGridView can also work with, e.g. a List<Person>, so you could:

var ps = new List<Person>();
ps.Add(new Person{Name = "John", Score = 90, Birthday = DateTime.Now});

peopleDataGridView.DataSource = ps;

You might have to jiggle the columns around though; a DGV will create columns in the order of the Columns collection if it's using a datatable, but it might not create the columns in the same order as properties appear in the source code of a Person

  • Related