1

I have a set of arrays in an array that contain characters e.g.
{{'#','.','#'}{'#','$','#'}{'#','@','#',}{'#','#','#',}}
And what I want to be able to create a grid based on the number of those dynamically as the amount can change (they will always have the same amount of characters in every internal array) so i want the internal arrays to be rows and the characters inside to each be on their own column so it would look like

"#.#"
"#$#"
"#@#"
"###"

so what would be the best way to setup this grid and then what i would like to do is fill all of those cells with pictureboxs as depending on what symbol it is it will be a specific picture.

so just wondering what the best way to do this would be any help/advice is much appreciated

1seanr
  • 657
  • 2
  • 7
  • 18
  • 1
    Using a `DataGridView` is the most simple option. – Reza Aghaei Oct 13 '16 at 06:52
  • Possible duplicate of [How to populate a WPF grid based on a 2-dimensional array](http://stackoverflow.com/questions/276808/how-to-populate-a-wpf-grid-based-on-a-2-dimensional-array) – rmbq Oct 13 '16 at 07:15

1 Answers1

1

Using DataGridView is the most simple option. For example:

var images = new Dictionary<char, Image>()
{
    {'#', Properties.Resources.Image1},
    {'.', Properties.Resources.Image2},
    {'$', Properties.Resources.Image3},
    {'@', Properties.Resources.Image4},
};
var data = new List<string>() { "#.#", "#$#", "#@#", "###" };
var list = data.Select(x => new
                       {
                           A = images[x[0]],
                           B = images[x[1]],
                           C = images[x[2]]
                       }).ToList();
this.dataGridView1.DataSource = list;

In above code, I created a dictionary for mapping between characters and images. Then shaped input data to a list containing that images instead of characters. When you set the result list as DataSource of DataGridView you will see those images instead of characters.


If for any reason you don't want to use anonymous object, you can simply create a dynamic DataTable having any number of columns:

var images = new Dictionary<char, Image>()
{
    {'#', Properties.Resources.Image1},
    {'.', Properties.Resources.Image2},
    {'$', Properties.Resources.Image3},
    {'@', Properties.Resources.Image4},
};
char[][] data = new char[][]{ 
    new char[] {'#','.','#'},
    new char[] {'#','$','#'},
    new char[] {'#','@','#'},
    new char[] {'#','#','#'},
};
var dt = new DataTable();
for (int i = 0; i < data.Max(x => x.Count()); i++)
    dt.Columns.Add(string.Format("C{0}", i), typeof(Image));
data.ToList().ForEach(a => dt.Rows.Add(a.Select(x=>images[x]).ToArray()));
this.dataGridView1.DataSource = dt;
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • This works really well thanks i had to do a bit of adapting but thanks so much the only problem though is it creates a row of white boxes with red "X"s at the bottom of it so I think its in that second to bottom line where it is creating 1 to many rows and therefor doesnt have anything to put in the cells but ill figure it out tomorrow – 1seanr Oct 13 '16 at 09:20
  • 1
    You'r welcome. Set `this.dataGridView1.AllowUserToAddRows = false;`. – Reza Aghaei Oct 13 '16 at 11:36