Linked Questions

Popular Questions

Creating grids dynamically in Xamarin.Forms

Asked by At

I am currently working an test application to list e.g. Products, but have the problem of not being able to generate the grids dynamically with the corresponding content (for now only labels). I want to generate them right when the Mainpage is called.

I have already gone throughout various tutorials and websites, but couldn't find anything that would help me save my problem. Tried to start the method for creating the grids by assigning it to a button. I have tried allocating the method to the constructor of the MainPage class, but still there won't be anything shown in the final result.

public void CreateDummyGrids()
    {
        Grid gOut = new Grid();

        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.RowDefinitions.Add(new RowDefinition());
        gOut.ColumnDefinitions.Add(new ColumnDefinition());
        gOut.ColumnDefinitions.Add(new ColumnDefinition());

        for (int rowIndex = 0; rowIndex < 3; rowIndex++)
        {
            for (int columnIndex = 0; columnIndex < 2; columnIndex++)
            {

                var label = new Label
                {
                   Text  ="Hello",
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                gOut.Children.Add(label, columnIndex, rowIndex);
            }
        }
    }

Related Questions