Xamarin.Forms - Dynamic GridView

Introduction

This article demonstrates how to create Dynamic GridView using Xamarin.Forms. If you ever need to show something in a data-bound grid view that is not in your data source, just create that extra row dynamically.

Dynamic GridView

Dynamic GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted into the layout using a ListAdapter. This GridView will fill the entire screen. The attributes are rather self-explanatory. For more information about valid attributes, see the GridView reference.



Let's start

Step 1

Create a Xamarin.Forms app by going to File >> New >> Visual C# >> Cross Platform >> Cross platform app (Xamarin.Native or Xamarin.Forms) and click OK. Project Name :DynamicGrid



Now, select Blank App >> Xamarin.Forms >> .Portable Class Library and click OK.

Step 2

After the project creation, add the following Nuget Packages for your projects. Open Solution Explorer >> right click to solution explorer and select "Manage NuGet packages for the solution".

Now, select the following NuGet packages and followed by select your project then click to Install it.
  • Xamarin.Forms update 2.5.0.121934.


Step 3

Next, open Solution Explorer >> Project Name(Portable) >> Right click to add >> New iteam . After that, open the Dialog Box Select Xamarin.Forms >> ContentPage and click ok (Page Name: DynamicGridpage ).



Step 4

Next, open Solution Explorer >> Project Name(Portable) >> DynamicGridPage.xaml. After that, open the Design View of this page. Just replace that the following code.

Grid View Name: gridLayout



Xaml Code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <ContentPage  
  4.       
  5.       
  6. xmlns="http://xamarin.com/schemas/2014/forms"  
  7.       
  8. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  9.       
  10. xmlns:local="clr-namespace:DynamicGrid"  
  11.     x:Class="DynamicGrid.DynamicGridPage">  
  12.   
  13. <Grid x:Name="gridLayout"></Grid>  
  14.         
  15. </ContentPage>  
Next, open Solution Explorer >> Project Name(Portable) >> DynamicGridPage.xaml >> DynamicGridPage.cs. After that, open the backend View of this page. Just replace that the following code.



14 line to 19 line Create an ArrayList and add product values, 21 to 25 line Define rows and columns.

Looping

C# for loop: The for keyword indicates a loop in C#. The for loop executes a block of statements repeatedly until the specified condition returns false. As per the syntax above, the for loop contains three parts: initialization, conditional expression, and steps, which are separated by a semicolon

  • Two for loop and one if condition
A fist is looped by only rows and then second is looped by columns.



CS Code using
  1.  Xamarin.Forms;  
  2. using System.Threading;  
  3. using System.Collections.Generic;  
  4.   
  5. namespace DynamicGrid  
  6. {  
  7.     public partial class DynamicGridPage : ContentPage  
  8.     {  
  9.         private List<Product> productArrayList;  
  10.   
  11.         public DynamicGridPage()  
  12.         {  
  13.             InitializeComponent();  
  14.             productArrayList = new List<Product>();  
  15.             productArrayList.Add(new Product { Name = "Mocca" });  
  16.             productArrayList.Add(new Product { Name = "Espresso" });  
  17.             productArrayList.Add(new Product { Name = "Latte" });  
  18.             productArrayList.Add(new Product { Name = "Americano" });  
  19.             productArrayList.Add(new Product { Name = "Arabica" });  
  20.   
  21.             gridLayout.RowDefinitions.Add(new RowDefinition());  
  22.             gridLayout.RowDefinitions.Add(new RowDefinition());  
  23.             gridLayout.ColumnDefinitions.Add(new ColumnDefinition());  
  24.             gridLayout.ColumnDefinitions.Add(new ColumnDefinition());  
  25.             gridLayout.ColumnDefinitions.Add(new ColumnDefinition());  
  26.   
  27.             var productIndex = 0;  
  28.             for (int rowIndex = 0; rowIndex < 2; rowIndex++)  
  29.             {  
  30.                 for (int columnIndex = 0; columnIndex < 3; columnIndex++)  
  31.                 {  
  32.                     if (productIndex >= productArrayList.Count)  
  33.                     {  
  34.                         break;  
  35.                     }  
  36.                     var product = productArrayList[productIndex];  
  37.                     productIndex += 1;  
  38.                     var label = new Label  
  39.                     {  
  40.                         Text = product.Name,  
  41.                         VerticalOptions = LayoutOptions.Center,  
  42.                         HorizontalOptions = LayoutOptions.Center  
  43.                     };  
  44.                     gridLayout.Children.Add(label, columnIndex, rowIndex);  
  45.                 }  
  46.             }  
  47.         }  
  48.     }  
  49. }  
Step 5

Next, Go to Solution Explorer >> Project Name (Portable) >> App.xaml >> App.xaml.cs click. Select the App page, The c-sharp code will appear, Just add that the following code.

Line 05 care full to handle, I have created xaml page. That name is DynamicGridPage
  1. public App()  
  2.         {  
  3.             InitializeComponent();  
  4.   
  5.             MainPage = new DynamicGridPage();  // this my xaml page just replace the your design page 
  6.         }  
Step 6

After completing your work with the design view, go to " Build " menu and click open "Configure Manager ". In the popup window, configure your startup projects. Click F5 or Run your project. Given below is the result

Output


Summary

Now, you have successfully tested Dynamic GridView in Xamarin.Forms application for Cross-Platform Application Development using Visual C# and Xamarin.