Blazor Data Grid Overview

    The Ignite UI for Blazor Data Table / Data Grid is a tabular Blazor grid component that allows you to quickly bind and display your data with little coding or configuration. Features of the Blazor data grid include filtering, sorting, templates, row selection, row grouping, row pinning and movable columns. The Blazor tables are optimized for live, streaming data, with the ability to handle unlimited data set size in number of rows or columns.

    Blazor Data Grid Example

    This demo implements some of the features that are available in the Grid: Filtering, Grouping, Pin/Unpin columns, Reposition columns, Sorting, and Summaries

    Getting Started

    Dependencies

    Please refer to these topics on adding the IgniteUI.Blazor package.

    Afterwards, you may start implementing the control by adding the following namespaces:

    @using IgniteUI.Blazor.Controls
    @inject IIgniteUIBlazor IgniteUIBlazor
    

    Required Modules

    The DataGrid requires the following modules:

    DataGridModule.Register(IgniteUIBlazor);
    

    Optional Modules

    The optional DataGrid features, seen above, requires the following modules:

    GridColumnOptionsModule.Register(IgniteUIBlazor);
    DataGridToolbarModule.Register(IgniteUIBlazor);
    SparklineModule.Register(IgniteUIBlazor);
    

    Sample Data Source

    Now that the Blazor data grid module is imported, next is the basic configuration of the Blazor grid that binds to local data.

    @code {
        public void GenerateData()
        {
            string[] names = new string[] {
                "Intel CPU", "AMD CPU",
                "Intel Motherboard", "AMD Motherboard", "Nvidia Motherboard",
                "Nvidia GPU", "Gigabyte GPU", "Asus GPU", "AMD GPU", "MSI GPU",
                "Corsair Memory", "Patriot Memory", "Skill Memory",
                "Samsung HDD", "WD HDD", "Seagate HDD", "Intel HDD", "Asus HDD",
                "Samsung SSD", "WD SSD", "Seagate SSD", "Intel SSD", "Asus SSD",
                "Samsung Monitor", "Asus Monitor", "LG Monitor", "HP Monitor" };
    
            string[] countries = new string[] {
                "USA", "UK", "France", "Canada", "Poland",
                "Denmark", "Croatia", "Australia", "Seychelles",
                "Sweden", "Germany", "Japan", "Ireland",
                "Barbados", "Jamaica", "Cuba", "Spain", };
            string[] status = new string[] { "Packing", "Shipped", "Delivered" };
    
            var sales = new List<SaleInfo>();
    
            for (var i = 0; i < 200; i++)
            {
                var price = GetRandomNumber(10000, 90000) / 100;
                var items = GetRandomNumber(4, 30);
                var value = Math.Round(price * items);
                var margin = GetRandomNumber(2, 5);
                var profit = Math.Round((price * margin / 100) * items);
                var country = GetRandomItem(countries);
    
                var item = new SaleInfo()
                {
                    Country = country,
                    CountryFlag = GetCountryFlag(country),
                    Margin = margin,
                    OrderDate = GetRandomDate(),
                    OrderItems = items,
                    OrderValue = value,
                    ProductID = 1001 + i,
                    ProductName = GetRandomItem(names),
                    ProductPrice = price,
                    Profit = Math.Round(profit),
                    Status = GetRandomItem(status)
                };
                sales.Add(item);
            }
    
            this.DataSource = sales;
        }
    
        public double GetRandomNumber(double min, double max)
        {
            return Math.Round(min + (Rand.NextDouble() * (max - min)));
        }
    
        public string GetRandomItem(string[] array)
        {
            var index = (int)Math.Round(GetRandomNumber(0, array.Length - 1));
            return array[index];
        }
    
        public DateTime GetRandomDate() {
            var today = new DateTime();
            var year = today.Year;
            var month = this.GetRandomNumber(1, 9);
            var day = this.GetRandomNumber(10, 27);
            return new DateTime(year, (int)month, (int)day);
        }
    
        public string GetCountryFlag(string country)
        {
            var flag = "https://static.infragistics.com/xplatform/images/flags/" + country + ".png";
            return flag;
        }
    
        public class SaleInfo
        {
            public string Status { get; set; }
            public string ProductName { get; set; }
            public string CountryFlag { get; set; }
            public string Country { get; set; }
            public DateTime OrderDate { get; set; }
            public double Profit { get; set; }
            public double ProductPrice { get; set; }
            public double ProductID { get; set; }
            public double OrderValue { get; set; }
            public double OrderItems { get; set; }
            public double Margin { get; set; }
        }
    }
    

    Auto-Generate Columns

    The following code demonstrates how to bind the Blazor data grid to the above local data.

     <DataGrid Height="100%"
        Width="100%"
        DataSource="DataSource"
        AutoGenerateColumns="true"
        DefaultColumnMinWidth="100"
        SummaryScope="DataSourceSummaryScope.Root"
        IsColumnOptionsEnabled="true"
        IsGroupCollapsable="true"
        GroupSummaryDisplayMode="GroupSummaryDisplayMode.RowBottom"
        ColumnMovingMode="ColumnMovingMode.Deferred"
        ColumnMovingAnimationMode="ColumnMovingAnimationMode.SlideOver"
        ColumnMovingSeparatorWidth="2"
        ColumnShowingAnimationMode="ColumnShowingAnimationMode.SlideFromRightAndFadeIn"
        ColumnHidingAnimationMode="ColumnHidingAnimationMode.SlideToRightAndFadeOut"
        SelectionMode="GridSelectionMode.SingleRow"
        CornerRadiusTopLeft="0"
        CornerRadiusTopRight="0" />
    

    Manually Define Columns

    <DataGrid Height="100%"
        Width="100%"
        DataSource="DataSource"
        AutoGenerateColumns="false">
        <NumericColumn Field="ProductID" HeaderText="Product ID" />
        <TextColumn Field="ProductName" HeaderText="Product Name" />
        <TextColumn Field="QuantityPerUnit" HeaderText="Quantity Per Unit" />
        <NumericColumn Field="UnitsInStock" HeaderText="Units In Stock" />
        <DateTimeColumn Field="OrderDate" HeaderText="Order Date" />
    </DataGrid>
    

    Styling Columns

    The following code demonstrates how to style specific columns using the provided column's properties.

    <TextColumn    
        Background="SkyBlue"
        FontStyle="italic" 
        FontWeight="bold" 
        FontFamily="Times New Roman" 
        FontSize="16"  
    />
    

    Additional Resources