Articles in this section
Category / Section

How to create a custom grid column in Blazor Grid?

5 mins read

This article explains how to create your own Custom Grid Column component in Blazor Grid.

In Blazor Grid, you can create a Custom GridColumn by inheriting the default GridColumn component.

In the following example, we have created a ReadOnlyGridColumn component by inheriting the GridColumn component.

In this ReadOnlyGridColumn, we have set AllowEditing for that column as false to make it as ReadOnly GridColumn while editing.

Find the following code snippets for your reference.

Index.razor

 
@using Syncfusion.Blazor.Grids
 
<SfGrid DataSource="@Orders" AllowPaging="true"
        Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })" AllowSorting="true">
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="EditMode.Normal"></GridEditSettings>
    <GridPageSettings PageSize="5"></GridPageSettings>
    <GridColumns>
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>
        <ReadOnlyGridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></ReadOnlyGridColumn>
        <GridColumn Field=@nameof(Order.OrderDate) HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn>
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"></GridColumn>
    </GridColumns>
</SfGrid>
 
@code{
    public List<Order> Orders { get; set; }
 
    protected override void OnInitialized()
    {
        Orders = Enumerable.Range(1, 75).Select(x => new Order()
        {
            OrderID = 1000 + x,
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)],
            Freight = 2.1 * x,
            OrderDate = DateTime.Now.AddDays(-x),
        }).ToList();
    }
 
    public class Order
    {
        public int? OrderID { get; set; }
        public string CustomerID { get; set; }
        public DateTime? OrderDate { get; set; }
        public double? Freight { get; set; }
    }
}

ReadOnlyGridColumn.razor

 
@using Syncfusion.Blazor.Grids
 
@inherits GridColumn
 
<CascadingValue Value="@this">
    @ChildContent
</CascadingValue>
 
@code{
 
    protected override void OnInitialized()
    {
        this.AllowEditing = false;
    }
}

Reference

Please find the below documentation links for more information.

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.GridColumn.html

https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.Grids.ColumnModel.html#Syncfusion_Blazor_Grids_ColumnModel_AllowEditing


Conclusion

I hope you enjoyed learning how to create a custom grid column in Blazor Grid.

You can refer to our Blazor DataGrid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor DataGrid example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied