Question

I have a dynamically created WPF Grid (rows and columns count based on layout) like this:

    private Grid CreateGrid(string layout)
    {
        int col;
        int row;
        if (layout == "4x4")
        {
            row = 4;
            col = 4;
        }
        else 
        {
            row = 2;
            col = 2;
        }

        Grid output = new Grid();

        for (int i = 0; i < col; i++)
        {
            ColumnDefinition coldef = new ColumnDefinition();
            coldef.Width = GridLength.Auto;
            output.ColumnDefinitions.Add(coldef);
        }

        for (int i = 0; i < row; i++)
        {
            RowDefinition rowdef = new RowDefinition();
            rowdef.Height = GridLength.Auto;
            output.RowDefinitions.Add(rowdef);
        }

        return output;
    }

In each of these "cells" should be another object, filled with data from dataSet (one table per object). That object is also another set of grids. My aim is to loop through this dynamically created grid and put this object into each cell. (and if there is layout 2x2 but only 3 tables in the dataset, I want only 3 object there and last cell would be empty)

However, I did not find anything like "foreach column in grid", or even how to get column / row count. On the other hand, all I found was related to DataGrid or GridView - what is the difference, and if the other type would be better to use for this, why?

Thanks in advance

Was it helpful?

Solution

So, I was able to find a solution to this. It might not be the best one, but it works.

Here is the code:

    foreach (DataTable table in result.Tables)
    {
        GfoTopBox box1 = StuffData(table);
        Grid.SetRow(box1, j);
        Grid.SetColumn(box1, i);
        output.Children.Add(box1);

        i++;
        if (i >= output.ColumnDefinitions.Count)
        {
            i = 0;
            j++;
        }
    }

"result" is the data set, each table provides the data for one of the objects I wanted to put in the cells of the Grid in the question (output).

However, if anyone has better solution, I am open to suggestions :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top