Dynamically create grid in Xamarin.Forms


Don Joe

I'm currently testing the app to list products, but I'm having an issue where the grid cannot be dynamically generated with the corresponding content (only for labels at the moment). I want to generate them as soon as the main page is called.

I've gone through various tutorials and websites but can't find anything that can help me with my problem. Try to start the method that creates the grid by assigning it to the button. I tried assigning the method to the constructor of the MainPage class, but still nothing shows up in the end 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);
            }
        }
    }
button

The Grid gOutvariable is local to your CreateDummyGridsmethod, not passed elsewhere. So after using the method, it will be destroyed.

You need to have some kind of element in the XAML to be able to add the grid to it (or just put the grid there and add the children directly to that grid).

So add the following where you want the grid to appear:

<Grid x:Name="productGrid" />

and change yours to CreateDummyGridsthis:

public void CreateDummyGrids()
    {
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.RowDefinitions.Add(new RowDefinition());
        productGrid.ColumnDefinitions.Add(new ColumnDefinition());
        productGrid.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
                };
                productGrid.Children.Add(label, columnIndex, rowIndex);
            }
        }
    }

Now keep in mind that every time this method is called, new ColumnDefinitions, RowDefinitions, and Labels are added, so if you want to keep adding stuff, you'll have to change it slightly. But I hope this is enough to get you going in the right direction

Related


Dynamically create grid in Xamarin.Forms

Don Joe I'm currently testing the app to list products, but I'm having an issue where the grid cannot be dynamically generated with the corresponding content (only for labels at the moment). I want to generate them as soon as the main page is called. I've gone

Dynamically create pages in Xamarin Forms

Avshin I have a text file like this in the cloud <Entry Placeholder ="FirstName" /> <Entry Placeholder ="Last Name" /> I want to load this file at runtime and inject these codes in the content page of Xamarin Forms Zafar If your xml file is not complicated an

Dynamically add BoxViews to grid [Xamarin.Forms]

Noel 625 I am trying to add BoxViews in Grid format with 3 columns and multiple rows. I have defined grid with behavior in xaml and c# files. What should happen is that a BoxView should be created for the same number of images, 3 images per column. thanks, XAM

Dynamically add BoxViews to grid [Xamarin.Forms]

Noel 625 I am trying to add BoxViews in Grid format with 3 columns and multiple rows. I have defined grid with behavior in xaml and c# files. What should happen is that a BoxView should be created for the same number of images, 3 images per column. thanks, XAM

Dynamically add BoxViews to grid [Xamarin.Forms]

Noel 625 I am trying to add BoxViews in Grid format with 3 columns and multiple rows. I have defined grid with behavior in xaml and c# files. What should happen is that a BoxView should be created for the same number of images, 3 images per column. thanks, XAM

Dynamically add BoxViews to grid [Xamarin.Forms]

Noel 625 I am trying to add BoxViews in Grid format with 3 columns and multiple rows. I have defined grid with behavior in xaml and c# files. What should happen is that a BoxView should be created for the same number of images, 3 images per column. thanks, XAM

How to create a grid on a button in Xamarin Forms

Loper So I need to create a custom button, on a grid I have to create, on this grid I need to create several labels with specific information. This is my code to add the child to the button private void HighlightTodayDay() { Label label1 = new

How to dynamically create click event on label in Xamarin Forms

Dipak: I'm working on a cross platform xamarin app and I want to create a hyperlink label for "Forgot your password?". on the login page. I have used the following code to create the label, but I don't know how to create the onclick event on the label. MainPa

How to dynamically create click event on label in Xamarin Forms

Deepak I'm working on a cross platform xamarin app and I want to create a hyperlink label for "Forgot your password?". on the login page. I have used the following code to create the label, but I don't know how to create the onclick event on the label. MainPa

How to dynamically create click event on label in Xamarin Forms

Dipak: I'm working on a cross platform xamarin app and I want to create a hyperlink label for "Forgot your password?". on the login page. I have used the following code to create the label, but I don't know how to create the onclick event on the label. MainPa

Xamarin: Build grid dynamically

Gigi I'm a new Xamarin/C# developer and I'm currently building an application that will display various enterprise services. I'm looking for advice on how to dynamically build a grid based on the number of services provided by an enterprise. For example, one b

Dynamically create and submit forms

Santosh Gokak: Is there a way in jQuery to dynamically create and submit a form. Like below. <html> <head> <title> Title Text Goes Here </title> <script src="http://code.jquery.com/jquery-1.7.js"></script> <script> $(document).ready(fun

Dynamically create forms and containers

SSD I am new to C#. Below is the code where I am trying to create the form and container in code ; but I am having problems. I start with a new Windows Forms Applicationtemplate . I changed the file Program.csslightly to be able to create the file FormMaindyna

Dynamically create forms

Suki I am facing a problem. I have a javascript function (using function append()) that generates a form. I'm basically trying to create a form with a submit button and when this button is pressed a php function is called which is responsible for doing somethi

Create forms dynamically or statically?

tdoakiiii I have a specific web page that needs about 6 different forms. Some of them contain up to 15 lines of unique labels and inputs. I made a javascript library that can accept large objects and output a form. Basically, it accepts an endless object like:

Dynamically create and submit forms

Santosh Gokak: Is there a way in jQuery to dynamically create and submit a form. Like below. <html> <head> <title> Title Text Goes Here </title> <script src="http://code.jquery.com/jquery-1.7.js"></script> <script> $(document).ready(fun

Dynamically create and submit forms

Luca Horvath I have some data in my model that I would like to put into the request body and send via a POST request. The problem is that I want the browser to navigate to the request URL because the route returns a new HTML page. Usually this would be done by

Dynamically create forms

Suki I am facing a problem. I have a javascript function (using function append()) that generates a form. I'm basically trying to create a form with a submit button and when this button is pressed a php function is called which is responsible for doing somethi

Dynamically create forms

Suki I am facing a problem. I have a javascript function (using function append()) that generates a form. I'm basically trying to create a form with a submit button and when this button is pressed a php function is called which is responsible for doing somethi

Dynamically create forms and containers

SSD I am new to C#. Below is the code where I am trying to create the form and container in code ; but I am having problems. I start with a new Windows Forms Applicationtemplate . I changed the file Program.csslightly to be able to create the file FormMaindyna

Create forms dynamically or statically?

tdoakiiii I have a specific web page that needs about 6 different forms. Some of them contain up to 15 lines of unique labels and inputs. I made a javascript library that can accept large objects and output a form. Basically, it accepts an endless object like:

Dynamically create and submit forms

Santosh Gokak: Is there a way in jQuery to dynamically create and submit a form. Like below. <html> <head> <title> Title Text Goes Here </title> <script src="http://code.jquery.com/jquery-1.7.js"></script> <script> $(document).ready(fun

Dynamically create forms

Suki I am facing a problem. I have a javascript function (using function append()) that generates a form. I'm basically trying to create a form with a submit button and when this button is pressed a php function is called which is responsible for doing somethi

Dynamically create forms

Suki I am facing a problem. I have a javascript function (using function append()) that generates a form. I'm basically trying to create a form with a submit button and when this button is pressed a php function is called which is responsible for doing somethi

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda

Dynamically create Bootstrap grid

Swopnilnep I'm trying to dynamically generate bootstrap columns using a javascript function. The purpose is to make them appear as different columns in the same row. It currently looks like this: I call this function on window.onload() with the div id: // Upda