Introduction

The DataGrid is a format or architecture to show a set of data in the user interface. In Xamarin Forms there is no default DataGrid control, so we need to install “DataGrid.Xamarin.Forms” dependency.
Xamarin.Forms - DataGrid
Let’s start to create new Xamarin.Forms application in Visual Studio for Mac
Xamarin.Forms - DataGrid
Once the project is created, install “Xamarin.Forms.DataGrid” dependency by going to the Solution Explorer >> right-click and select “Manage NuGet Packages” >> in the new dialog, in the top right corner search for “Xamarin.Forms.DataGrid” and install it.
Xamarin.Forms - DataGrid
Create an MVVM folder structure, create a Professional.cs model class under the Model folder. For that, open Solution Explorer >> right-click the Model folder and select Add > new class and give name as Professional.cs. The class code is given below.
  1. using System;
  2. namespace XFDataGrid.Models
  3. {
  4. public class Professional
  5. {
  6. public string Id { get; set; }
  7. public string Name { get; set; }
  8. public string Desigination { get; set; }
  9. public string Domain { get; set; }
  10. public string Experience { get; set; }
  11. }
  12. }
Now, create another DummyProfessionalData.cs class under the Utils folder for dummy data. This class used for creating dummy data and code is given here.
  1. using System;
  2. using System.Collections.Generic;
  3. using XFDataGrid.Models;
  4. namespace XFDataGrid.Utils
  5. {
  6. public static class DummyProfessionalData
  7. {
  8. public static List<Professional> GetProfessionals()
  9. {
  10. var data = new List<Professional>();
  11. var person = new Professional()
  12. {
  13. Id = "3",
  14. Name = "Monkey",
  15. Desigination = "Developer",
  16. Domain = "Mobile",
  17. Experience = "1"
  18. };
  19. for (int i = 0; i < 10; i++)
  20. {
  21. data.Add(person);
  22. }
  23. return data;
  24. }
  25. }
  26. }
Next, Create MainPageViewModel.cs class under ViewModel folder and write the below code. Here, List and IsRefreshing properties and refresh command are written, and when the user swipes top to bottom, the refresh command will execute and refresh the UI with updated data. INotifyPropertyChanged property is also implemented to invoke the UI with new Data. The Professional Property is for single data selection.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Threading.Tasks;
  5. using System.Windows.Input;
  6. using Xamarin.Forms;
  7. using XFDataGrid.Models;
  8. using XFDataGrid.Utils;
  9. namespace XFDataGrid.ViewModels
  10. {
  11. public class MainPageViewModel : INotifyPropertyChanged
  12. {
  13. private List<Professional> _professionals;
  14. private Professional _selectedProfessional;
  15. private bool _isRefreshing;
  16. public List<Professional> Professionals
  17. {
  18. get
  19. {
  20. return _professionals;
  21. }
  22. set
  23. {
  24. _professionals = value;
  25. OnPropertyChanged(nameof(Professionals));
  26. }
  27. }
  28. public Professional SelectedProfesstional
  29. {
  30. get
  31. {
  32. return _selectedProfessional;
  33. }
  34. set
  35. {
  36. _selectedProfessional = value;
  37. OnPropertyChanged(nameof(SelectedProfesstional));
  38. }
  39. }
  40. public bool IsRefreshing
  41. {
  42. get
  43. {
  44. return _isRefreshing;
  45. }
  46. set
  47. {
  48. _isRefreshing = value;
  49. OnPropertyChanged(nameof(IsRefreshing));
  50. }
  51. }
  52. public ICommand RefreshCommand { get; set; }
  53. public MainPageViewModel()
  54. {
  55. Professionals = DummyProfessionalData.GetProfessionals();
  56. RefreshCommand = new Command(CmdRefresh);
  57. }
  58. private async void CmdRefresh()
  59. {
  60. IsRefreshing = true;
  61. await Task.Delay(3000);
  62. IsRefreshing = false;
  63. }
  64. public event PropertyChangedEventHandler PropertyChanged;
  65. private void OnPropertyChanged(string property)
  66. {
  67. if (PropertyChanged != null)
  68. PropertyChanged(this, new PropertyChangedEventArgs(property));
  69. }
  70. }
  71. }
After that open MainPage.xaml page and start our UI design. First, we call DataGrid namespace as dg and design our columns with Title, bind the PropertyName and width size. Bind ItemSource, SelectedItem, refershCommand. This page design code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:d="http://xamarin.com/schemas/2014/forms/design"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:dg="clr-namespace:Xamarin.Forms.DataGrid;assembly=Xamarin.Forms.DataGrid"
  7. x:Class="XFDataGrid.MainPage">
  8. <ContentView>
  9. <!-- Place new controls here -->
  10. <dg:DataGrid ItemsSource="{Binding Professionals}" SelectionEnabled="True" SelectedItem="{Binding SelectedProfesstional}" RowHeight="70" HeaderHeight="50"
  11. BorderColor="#CCCCCC" HeaderBackground="#E0E6F8" PullToRefreshCommand="{Binding RefreshCommand}" IsRefreshing="{Binding IsRefreshing}" ActiveRowColor="#8899AA">
  12. <x:Arguments>
  13. <ListViewCachingStrategy>RetainElement</ListViewCachingStrategy>
  14. </x:Arguments>
  15. <dg:DataGrid.HeaderFontSize>
  16. <OnIdiom x:TypeArguments="x:Double">
  17. <OnIdiom.Tablet>15</OnIdiom.Tablet>
  18. <OnIdiom.Phone>12</OnIdiom.Phone>
  19. </OnIdiom>
  20. </dg:DataGrid.HeaderFontSize>
  21. <dg:DataGrid.Columns>
  22. <dg:DataGridColumn Title="ID" PropertyName="Id" Width="1*"></dg:DataGridColumn>
  23. <dg:DataGridColumn PropertyName="Name" Width="2*">
  24. <dg:DataGridColumn.FormattedTitle>
  25. <FormattedString>
  26. <Span Text="Name" FontSize="13" TextColor="Black" FontAttributes="Bold"/>
  27. </FormattedString>
  28. </dg:DataGridColumn.FormattedTitle>
  29. </dg:DataGridColumn>
  30. <dg:DataGridColumn Title="Desigination" PropertyName="Desigination" Width="2*"/>
  31. <dg:DataGridColumn Title="Domain" PropertyName="Domain" Width="2*"/>
  32. <dg:DataGridColumn Title="Exp" PropertyName="Experience" Width="1*"/>
  33. </dg:DataGrid.Columns>
  34. <dg:DataGrid.RowsBackgroundColorPalette>
  35. <dg:PaletteCollection>
  36. <Color>#F2F2F2</Color>
  37. <Color>#FFFFFF</Color>
  38. </dg:PaletteCollection>
  39. </dg:DataGrid.RowsBackgroundColorPalette>
  40. </dg:DataGrid>
  41. </ContentView>
  42. </ContentPage>
Next open MainPage.xaml.cs and set BindingContext is MainPageViewModel as we done already. The source code is here.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Xamarin.Forms;
  8. using XFDataGrid.ViewModels;
  9. namespace XFDataGrid
  10. {
  11. // Learn more about making custom code visible in the Xamarin.Forms previewer
  12. // by visiting https://aka.ms/xamarinforms-previewer
  13. [DesignTimeVisible(false)]
  14. public partial class MainPage : ContentPage
  15. {
  16. public MainPage()
  17. {
  18. InitializeComponent();
  19. BindingContext = new MainPageViewModel();
  20. }
  21. }
  22. }
Finally, initialize DataComponent in App Startup of App.xaml.cs class like the below code.
  1. using System;
  2. using Xamarin.Forms;
  3. using Xamarin.Forms.Xaml;
  4. namespace XFDataGrid
  5. {
  6. public partial class App : Application
  7. {
  8. public App()
  9. {
  10. Xamarin.Forms.DataGrid.DataGridComponent.Init();
  11. InitializeComponent();
  12. MainPage = new MainPage();
  13. }
  14. protected override void OnStart()
  15. {
  16. }
  17. protected override void OnSleep()
  18. {
  19. }
  20. protected override void OnResume()
  21. {
  22. }
  23. }
  24. }
Now, run your application and output like the below screenshot.
Xamarin.Forms - DataGrid
The full source code is here - GitHub

Conclusion

I hope you all now understand how to use DataGrid in Xamarin.Forms and this blog is helpful for all. Thanks for reading.