Showing posts with label datagrid. Show all posts

Introduction Welcome to our newest blog post, where we explore the vibrant realm of app development with .NET MAUI! In this deta...

Dev Express - Data Grid Control for .NET MAUI (Lifetime - Free plugin) Dev Express - Data Grid Control for .NET MAUI (Lifetime - Free plugin)

A blog about android developement

datagrid

Introduction

Welcome to our newest blog post, where we explore the vibrant realm of app development with .NET MAUI! In this detailed guide, we dive into the intricacies of the "DataGrid Control for .NET MAUI," an outstanding free plugin crafted to simplify your data management process. Uncover how this plugin enables developers to effortlessly sort, filter, and present data, elevating user experiences across cross-platform applications.


Telerik, SyncFusion, and DevExpress offer paid licenses, often accompanied by trial versions or community licenses under specific terms. However, DevExpress stands out by providing a set of controls that you can use absolutely free!. As the demand for seamless data visualization and interaction grows, this free plugin emerges as a game-changer for .NET MAUI enthusiasts.


How can I obtain a free license for DevExpress?

  • Visit the following link FREE Xamarin UI Controls
  • Click "GET IT FREE" button in the screen, will redirect to another page.
  • In this page, you need to enter your details and valid mail id.
  • After the successful submission, the DevExpress team will send the nuget api link to the entered email id.

Quick Links:


Project Setup:

  • Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
  • In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
  • In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
  • In the Additional information window, click the Create button:
  • Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app

Install Plugin:

In this steps, we will see the steps to install "DevExpress Grid" in Visual Studio 2022:
  • Access NuGet Package Manager: In Visual Studio, right-click on your .NET MAUI project in the Solution Explorer. From the context menu, select "Manage NuGet Packages."
  • Search for "DevExpress.XamarinForms.DataGrid": In the NuGet Package Manager, click on the "Browse" tab. In the search bar, type "DevExpress.XamarinForms.DataGrid" and hit Enter. The package should appear in the search results.
  • Select and Install the Package: Once you find "DevExpress.XamarinForms.DataGrid" in the search results, click on it to select it. Ensure you choose the appropriate version compatible with your .NET MAUI project. Click on the "Install" button to begin the installation process.
  • Accept License Agreement: During the installation, you may be prompted to accept the license agreement. Review the terms and conditions and click on the "Accept" button to proceed.
  • Wait for Installation to Complete: Visual Studio will download and install the package along with its dependencies. This process may take a few moments, depending on your internet connection speed.
  • Verify Installation: After the installation is complete, verify that there are no error messages in the Output window. This indicates a successful installation of the package.

Implementation

Open MauiProgram.cs file, and add ".UseDevExpress()" in builder like below

var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseDevExpress()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});

Add Namespace in XAML:

In your .NET MAUI XAML files where you intend to use the Dev Express - DataGrid, make sure to add the appropriate XML namespace. For example:

xmlns:dxg="clr-namespace:DevExpress.Maui.DataGrid;assembly=DevExpress.Maui.DataGrid"
xmlns:dxe="clr-namespace:DevExpress.Maui.Editors;assembly=DevExpress.Maui.Editors"
xmlns:dxc="clr-namespace:DevExpress.Maui.Controls;assembly=DevExpress.Maui.Controls"

Adjust the namespace and assembly name based on the specifics of the DataGrid component you installed.


Implement DataGrid in Your XAML:

You can now implement the DataGrid control in your XAML files using the namespace you added. For example:

<dg:DataGrid RefreshingEnabled="True"
             BackgroundColor="White"
                     IsRefreshing="{Binding Refreshing}"
                     PullToRefreshCommand="{Binding OnDataGridRefreshCommand}"
                     SelectionEnabled="True"
                     SelectedItem="{Binding SelectedItem}"
                     RowHeight="70"
                     HeaderHeight="70"
                     ItemsSource="{Binding Items}"
                     HeaderBackground="Red">

    <dg:DataGrid.NoDataView>
        <Label Text="Nothing to see here" HorizontalOptions="Center" VerticalOptions="Center" />
    </dg:DataGrid.NoDataView>
    <dg:DataGrid.Columns>
        <dg:DataGridColumn Title="Image" PropertyName="Image" Width="150" SortingEnabled="False">
            <dg:DataGridColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding}" HorizontalOptions="Center" VerticalOptions="Center"
                           Aspect="AspectFit" HeightRequest="60" />
                </DataTemplate>
            </dg:DataGridColumn.CellTemplate>
        </dg:DataGridColumn>
        <dg:DataGridColumn Title="Name" PropertyName="Name" Width="100"/>
        <dg:DataGridColumn Title="Location" PropertyName="Location"  Width="100"/>
        <dg:DataGridColumn Title="Population" PropertyName="Population"  Width="100"/>
        <dg:DataGridColumn Title="Latitude" PropertyName="Latitude"  Width="100"/>
        <dg:DataGridColumn Title="Longitude" PropertyName="Longitude"  Width="100"/>
    </dg:DataGrid.Columns>
    <dg:DataGrid.RowsBackgroundColorPalette>
        <dg:PaletteCollection>
            <Color>#e1e1e1</Color>
            <Color>#ffffff</Color>
        </dg:PaletteCollection>
    </dg:DataGrid.RowsBackgroundColorPalette>
</dg:DataGrid>

The above code snippet represents a DataGrid component in .NET MAUI application. Let's break down the properties and their meanings in the context of this DataGrid:

  • ItemsSource="{Binding Items}": Binds the DataGrid to a collection of items represented by the "Items" property in the ViewModel. Each item in this collection corresponds to a row in the DataGrid.
  • dxg:DataGridView.Columns: Specifies the columns to be displayed in the DataGrid.
  • dxg:TemplateColumn: We can use the custom templated column
  • dxg:TextColumn: Used to display the values in the column
  • dxg:DateColumn: Used to display the date values with datepicker selection in the column
  • dxg:ComoboxColumn: Used to display the values with dropdown selection in the column
  • dxg:CheckBoxColumn : Used to display the boolen selection with checkbox in the column

The below code snippet configures a DataGrid component with specific properties and templates, allowing users to view and interact with data in a visually appealing manner.


The Code Behind or MVVM view model properties are similar to listview control and no specific code required

using System.Collections.ObjectModel;
using System.Net.Http;
using System.Net.Http.Json;

namespace MauiDevExpress
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            BindingContext = this;
            InitializeComponent();
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            await Task.Delay(2000);
            LoadData();
        }

        private void LoadData()
        {
            var monkeys = new List<Monkey>
            {
                new Monkey
                {
                    Name = "Chimpanzee",
                    Location = "Africa",
                    Details = "Chimpanzees are intelligent primates known for their problem-solving abilities.",
                    Image = "chimpanzee.jpg",
                    Population = 150000,
                    Latitude = -1.2921f,
                    Longitude = 36.8219f,
                    BirthDate = new DateTime(2000, 1, 15),
                    AccessLevel = AccessLevel.Admin
                },
                new Monkey
                {
                    Name = "Orangutan",
                    Location = "Borneo",
                    Details = "Orangutans are great apes native to the rainforests of Borneo and Sumatra.",
                    Image = "orangutan.jpg",
                    Population = 70000,
                    Latitude = 1.3521f,
                    Longitude = 110.4647f,
                    BirthDate = new DateTime(2005, 3, 20),
                    AccessLevel = AccessLevel.User
                },
                new Monkey
                {
                    Name = "Gorilla",
                    Location = "Africa",
                    Details = "Gorillas are the largest primates and share about 98.3% of their DNA with humans.",
                    Image = "gorilla.jpg",
                    Population = 100000,
                    Latitude = -0.2280f,
                    Longitude = 15.8277f,
                    BirthDate = new DateTime(1998, 7, 10),
                    AccessLevel = AccessLevel.Admin
                },
                // Add more monkey instances with BirthDate and AccessLevel properties
                new Monkey
                {
                    Name = "Howler Monkey",
                    Location = "South America",
                    Details = "Howler monkeys are known for their loud vocalizations that can be heard up to 3 miles away.",
                    Image = "howler_monkey.jpg",
                    Population = 50000,
                    Latitude = -14.2350f,
                    Longitude = -51.9253f,
                    BirthDate = new DateTime(2002, 5, 3),
                    AccessLevel = AccessLevel.User
                },
                new Monkey
                {
                    Name = "Capuchin Monkey",
                    Location = "Central and South America",
                    Details = "Capuchin monkeys are highly intelligent and are often used in scientific research.",
                    Image = "capuchin_monkey.jpg",
                    Population = 30000,
                    Latitude = 4.7100f,
                    Longitude = -74.0721f,
                    BirthDate = new DateTime(2007, 9, 18),
                    AccessLevel = AccessLevel.Admin
                },
                new Monkey
                {
                    Name = "Spider Monkey",
                    Location = "Central and South America",
                    Details = "Spider monkeys are known for their long limbs and prehensile tail, which they use to swing through trees.",
                    Image = "spider_monkey.jpg",
                    Population = 25000,
                    Latitude = 14.634915f,
                    Longitude = -90.506882f,
                    BirthDate = new DateTime(1999, 11, 7),
                    AccessLevel = AccessLevel.User
                },
                new Monkey
                {
                    Name = "Mandrill",
                    Location = "Africa",
                    Details = "Mandrills are colorful monkeys found in the rainforests of Central Africa.",
                    Image = "mandrill.jpg",
                    Population = 10000,
                    Latitude = 0.2280f,
                    Longitude = 14.8277f,
                    BirthDate = new DateTime(2004, 2, 14),
                    AccessLevel = AccessLevel.Admin
                },
                new Monkey
                {
                    Name = "Tarsier",
                    Location = "Southeast Asia",
                    Details = "Tarsiers are small primates known for their big eyes and unique hunting skills.",
                    Image = "tarsier.jpg",
                    Population = 5000,
                    Latitude = 9.3275f,
                    Longitude = 123.3076f,
                    BirthDate = new DateTime(2001, 8, 22),
                    AccessLevel = AccessLevel.User
                },
                new Monkey
                {
                    Name = "Golden Lion Tamarin",
                    Location = "Brazil",
                    Details = "Golden lion tamarins are endangered primates with striking orange fur and manes.",
                    Image = "golden_lion_tamarin.jpg",
                    Population = 1500,
                    Latitude = -22.9068f,
                    Longitude = -43.1729f,
                    BirthDate = new DateTime(2006, 12, 5),
                    AccessLevel = AccessLevel.Admin
                },
                new Monkey
                {
                    Name = "Proboscis Monkey",
                    Location = "Borneo",
                    Details = "Proboscis monkeys have large noses and are excellent swimmers.",
                    Image = "proboscis_monkey.jpg",
                    Population = 700,
                    Latitude = 2.4604f,
                    Longitude = 115.3502f,
                    BirthDate = new DateTime(2003, 4, 30),
                    AccessLevel = AccessLevel.User
                },
                // Add more monkey instances as needed
                // ...
            };

            dxg.ItemsSource = new ObservableCollection<Monkey>(monkeys);
        }


        public class Monkey
        {
            public string Name { get; set; }
            public string Location { get; set; }
            public string Details { get; set; }
            public string Image { get; set; }
            public int Population { get; set; }
            public float Latitude { get; set; }
            public float Longitude { get; set; }
            public AccessLevel AccessLevel { get; set; }
            public DateTime BirthDate { get; set; }
        }
        public enum AccessLevel
        {
            Admin,
            User
        }
    }

}

Full Code:

Demo

Download Code:

You can download the code from GitHub. If you have any doubts, feel free to post a comment. If you liked this article, and it is useful to you, do like, share the article & star the repository on GitHub.

References

To learn more about DevExpress DataGrid