Skip to main content
All docs
V26.1
  • Columns in Blazor Grid

    • 13 minutes to read

    DevExpress Blazor Grid supports the following column types: data column (bound and unbound), command column, and selection column.

    DxGrid generates user-friendly captions for all data columns. Caption text is based on the bound data field name. Use the Caption property to change the generated caption.

    Data Column (Bound)

    Bound columns obtain their data from data source fields. Declare a DxGridDataColumn object in the Columns tag and specify the FieldName property to bind the column to a data field.

    Note

    The FieldName property value must be unique for each data column.

    @inject WeatherForecastService ForecastService
    
    <DxGrid Data="@Data">
        <Columns>
            <DxGridDataColumn FieldName="Date" DisplayFormat="D" />
            <DxGridDataColumn FieldName="TemperatureC" Caption="@("Temp. (\x2103)")" Width="120px" />
            <DxGridDataColumn FieldName="TemperatureF" Caption="@("Temp. (\x2109)")" Width="120px" />
            <DxGridDataColumn FieldName="Forecast" />
            <DxGridDataColumn FieldName="CloudCover" />
        </Columns>
    </DxGrid>
    
    @code {
        object Data { get; set; }
    
        protected override void OnInitialized() {
            Data = ForecastService.GetForecast();
        }
    }
    

    Blazor Grid Data Binding

    Run Demo: Grid - Data Binding Read Tutorial: Bind Blazor Grid to Data

    Data Column (Unbound)

    Unbound columns display values that are not stored in the assigned data collection. To create an unbound column, declare a DxGridDataColumn object in the Columns tag and specify the following properties:

    UnboundType
    Indicates that the column is unbound and specifies its data type.
    FieldName
    Specifies a unique name used to identify the column. This name must not match field names in the data source.

    You can use one of the following APIs to populate an unbound column with data:

    UnboundExpression property
    Specifies an expression used to calculate column values. An expression can consist of field names, constants, operators, and functions and should use criteria language syntax.
    UnboundColumnData event
    Allows you to implement custom logic or obtain column values from a custom/external data source.
    <DxGrid Data="forecasts" UnboundColumnData="Grid_CustomUnboundColumnData">
        <Columns>
            <DxGridDataColumn FieldName="Date" Caption="Date" />
            <DxGridDataColumn FieldName="TemperatureC" Caption="@("Temperature (\x2103)")" />
            <DxGridDataColumn FieldName="TemperatureF" Caption="@("Temperature (\x2109)")"
                              UnboundType="GridUnboundColumnType.Decimal"
                              UnboundExpression="32 + [TemperatureC] / 0.5556" />
            <DxGridDataColumn FieldName="Summary"
                              UnboundType="GridUnboundColumnType.String" />
        </Columns>
    </DxGrid>
    
    @code {
        private WeatherForecast[]? forecasts;
    
        protected override async Task OnInitializedAsync() {
            forecasts = await ForecastService.GetForecastAsync(DateTime.Now);
        }
    
        void Grid_CustomUnboundColumnData(GridUnboundColumnDataEventArgs e) {
            if (e.FieldName == "Summary") {
                int temperature = Convert.ToInt32(e.GetRowValue("TemperatureC"));
                e.Value = GetTemperatureString(temperature);
            }
        }
    
        static string GetTemperatureString(int value) {
            if (value < -10)
                return "Cool";
            if (value >= -10 && value < 5)
                return "Chilly";
            if (value >= 5 && value < 15)
                return "Warm";
            return "Hot";
        }
    }
    

    DevExpress Blazor Grid - Unbound Columns

    View Example: Create and Edit Unbound Columns

    Run Demo: Unbound Columns

    Limitations

    Command Column

    Declare a DxGridCommandColumn object in the Columns tag to display a command column:

    <DxGrid Data="DataSource" KeyFieldName="ProductId">
        <Columns>
            <DxGridCommandColumn />
            <DxGridDataColumn FieldName="FirstName" />
            <DxGridDataColumn FieldName="LastName" />
            <DxGridDataColumn FieldName="Title" />
        </Columns>
    </DxGrid>
    

    A command column contains CRUD-related buttons (New, Edit, and Delete) and the Clear button that resets values in the filter row. For edited rows in EditRow/EditCell mode, the column displays Save and Cancel buttons. DisplayMode specifies whether command buttons display icons, captions, or both.

    Blazor Grid Command Column

    Run Demo: Edit Row Run Demo: Filter Row Read Tutorial: Edit Data in Blazor Grid

    The following properties allow you to hide unnecessary command buttons:

    Selection Column

    Declare a DxGridSelectionColumn object in the Columns tag to display a selection column:

    <DxGrid Data="Products" KeyFieldName="ProductId">
        <Columns>
            <DxGridSelectionColumn />
            <DxGridDataColumn FieldName="ProductName" />
            <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" />
            <DxGridDataColumn FieldName="UnitsInStock" Caption="Units in Stock" />
            <DxGridDataColumn FieldName="QuantityPerUnit" Caption="Quantity per Unit" />
            <DxGridDataColumn FieldName="Discontinued" />
        </Columns>
    </DxGrid>
    

    Run Demo: Selection Column Read Tutorial: Selection and Focus in Blazor Grid

    Single Selection

    When the SelectionMode property is set to Single, the selection column displays radio buttons. Users can click a button to select one row at a time.

    <DxGrid Data="Products" KeyFieldName="ProductId" SelectionMode="GridSelectionMode.Single">
        <Columns>
            <DxGridDataColumn FieldName="ProductName" />
            <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" />
            <DxGridDataColumn FieldName="UnitsInStock" Caption="Units in Stock" />
            <DxGridDataColumn FieldName="QuantityPerUnit" Caption="Quantity per Unit" />
            <DxGridDataColumn FieldName="Discontinued" />
        </Columns>
    </DxGrid>
    

    Blazor Grid Selection Column Single Mode

    Multiple Selection

    When the SelectionMode property is set to Multiple (the default value), the selection column displays checkboxes. Users can click them to select and deselect individual rows.

    Blazor Grid Selection Column Multiple Mode

    In Multiple selection mode, the selection column displays the Select All checkbox in the column header. A user can click this checkbox to select or deselect all rows on the current page or on all grid pages depending on the SelectAllCheckboxMode property value. Available property values are as follows:

    Page
    The Select All checkbox selects and deselects all rows on the current grid page. This mode changes to Mixed when the Grid is bound to a Server Mode or Queryable data source and vertical virtual scrolling mode is activated.
    AllPages
    The Select All checkbox selects and deselects all rows on all grid pages. This mode changes to Mixed when the Grid is bound to a Server Mode or Queryable data source.
    Mixed
    The Select All checkbox selects and deselects all rows on the current grid page. An additional drop-down button displays a context menu that allows users to select and deselect all rows on all grid pages.
    <DxGrid Data="Products" 
            KeyFieldName="ProductId" 
            SelectAllCheckboxMode="GridSelectAllCheckboxMode.Mixed">
        <Columns>
            <DxGridDataColumn FieldName="ProductName" />
            <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" />
            <DxGridDataColumn FieldName="UnitsInStock" Caption="Units in Stock" />
            <DxGridDataColumn FieldName="QuantityPerUnit" Caption="Quantity per Unit" />
            <DxGridDataColumn FieldName="Discontinued" />
        </Columns>
    </DxGrid>
    

    Blazor Grid Select All Checkbox

    To hide the Select All checkbox, disable the AllowSelectAll option.

    Note

    The Select All checkbox functionality has limitations. For additional information, refer to the following section: Selection Limitations.

    Band Column (Header Bands)

    You can combine columns into logical groups called bands. To create such a group, declare a DxGridBandColumn object and specify child columns in the Columns tag. You can create as many nesting levels as your business task requires.

    <DxGrid @ref="Grid"
            Data="Data">
        <Columns>
            <DxGridDataColumn FieldName="SalesPerson" Caption="Salesperson" />
            <DxGridBandColumn Caption="Order">
                <Columns>
                    <DxGridDataColumn FieldName="CompanyName" />
                    <DxGridDataColumn FieldName="OrderDate" Caption="Date" Width="100px" />
                    <DxGridBandColumn Caption="Product">
                        <Columns>
                            <DxGridDataColumn FieldName="ProductName" Caption="Name" />
                            <DxGridDataColumn FieldName="UnitPrice" 
                                              DisplayFormat="c" 
                                              CaptionAlignment="GridTextAlignment.Right" 
                                              Width="100px" />
                        </Columns>
                    </DxGridBandColumn>
                    <DxGridDataColumn FieldName="Quantity" Width="80px" />
                </Columns>
            </DxGridBandColumn>
        </Columns>
    </DxGrid>
    

    Multi-level headers

    Run Demo: Grid - Header Bands

    Task-Based Examples

    Display an Image Column

    To display an image from a binary source, place an <img> element into the CellDisplayTemplate and specify the image source:

    <DxGridDataColumn FieldName="ImageData">
        <CellDisplayTemplate>
            <img style="width: 300px;" src="@GetImageSource(context)" />
        </CellDisplayTemplate>
    </DxGridDataColumn>
    
    const string ImageSourceFormat = "data:image/gif;base64,{0}";
    
    void GetImageSource(GridCellDisplayTemplateContext context) {
        return string.Format(ImageSourceFormat, Convert.ToBase64String((byte[])context.Value));
    }
    

    Create a Foreign Key (ComboBox/Lookup) Column

    A foreign key is a database key that manages relationships between tables. It identifies a column in a referenced table and lets you access that column’s data. Follow the steps below to create such a column:

    1. Add a DxGridDataColumn object to the Columns tag.
    2. Assign the field name that stores foreign keys to the column’s FieldName property.
    3. Place DxComboBoxSettings into the column’s EditSettings tag.
    4. Assign an external data source to the Data property.
    5. Assign the name of the external data source field that stores foreign keys to the ValueFieldName property. The TextFieldName property allows you to specify text strings displayed within the Grid instead of foreign keys.

    Combobox column

    <DxGrid Data="Products"
            EditModelSaving="OnEditModelSaving"
            EditMode="GridEditMode.EditRow">
        <Columns>
            <DxGridCommandColumn DeleteButtonVisible="false" />
            <DxGridDataColumn FieldName="CategoryId" Caption="Category Name">
                <EditSettings>
                    <DxComboBoxSettings
                        Data="Categories"
                        ValueFieldName="CategoryId"
                        TextFieldName="CategoryName"
                        FilteringMode="DataGridFilteringMode.Contains"
                        ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"/>
                </EditSettings>
            </DxGridDataColumn>
            <DxGridDataColumn FieldName="ProductName" />
            <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" />
            <DxGridDataColumn FieldName="UnitsInStock" />
            <DxGridDataColumn FieldName="QuantityPerUnit" />
        </Columns>
    </DxGrid>
    
    @code {
        NorthwindContext Northwind { get; set; }
        List<Product> Products { get; set; }
        List<Category> Categories { get; set; }
        @* ... *@
        async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {  
            var editableProduct = (Product)e.EditModel;
            e.CopyChangesToDataItem();
            await Northwind.SaveChangesAsync();
    
            Products = await Northwind.Products.ToListAsync();
        }
        @* ... *@
    }
    

    View Example: Create a Foreign Key (ComboBox) Column

    Create Columns at Runtime

    The following example creates columns at runtime. Note: you can combine columns declared in markup and created at runtime.

    @using System.Reflection
    @using System.ComponentModel 
    @inject EmployeeService EmployeeData
    
    <DxGrid Data="employees">
        <Columns>
            <DxGridCommandColumn />
            @BuildGridColumns(typeof(Employee))
        </Columns>
    </DxGrid>
    
    @code {
        IEnumerable<Employee> employees;
        protected override async Task OnInitializedAsync() {
            employees = await EmployeeData.GetData();
        }
        RenderFragment BuildGridColumns(Type itemType) {
        var props = TypeDescriptor.GetProperties(itemType);
        return b => {
          foreach(PropertyDescriptor prop in props) {
            b.OpenComponent(0, typeof(DxGridDataColumn));
            b.AddAttribute(1, "FieldName", prop.Name);
            b.CloseComponent();
          }
        };
      }
    }
    

    Checkbox Setting

    Display Values of Two Fields in One Column

    Implement an unbound column to merge data from two data fields.

    In the following example, the Grid data source contains FirstName and LastName fields. To display full names in a column, create an unbound column and set its UnboundType property to String. You can calculate column values in two ways:

    • Create FirstName and LastName hidden columns and concatenate their values in the UnboundExpression property:

      <DxGrid Data="employees" >
          <Columns>
              <DxGridDataColumn FieldName="FirstName" Visible="false" />
              <DxGridDataColumn FieldName="LastName" Visible="false" />
              <DxGridDataColumn FieldName="FullName" UnboundType="GridUnboundColumnType.String" 
                                UnboundExpression="[FirstName]+ ' ' +[LastName]" />
              <DxGridDataColumn FieldName="HireDate" />
              <DxGridDataColumn FieldName="Email" />
          </Columns>
      </DxGrid>
      
      @code {
          Employee[]? employees;
          protected override async Task OnInitializedAsync() {
              employees = await EmployeeData.GetData();
          }
      }
      
    • Handle the UnboundColumnData event and call the GetRowValue(String) method to access values of the FirstName and LastName data fields.

      <DxGrid Data="employees" UnboundColumnData="Grid_UnboundColumnData">
          <Columns>
              <DxGridDataColumn FieldName="FullName" UnboundType="GridUnboundColumnType.String" />
              <DxGridDataColumn FieldName="HireDate" />
              <DxGridDataColumn FieldName="Email" />
          </Columns>
      </DxGrid>
      
      @code {
          Employee[]? employees;
          protected override async Task OnInitializedAsync() {
              employees = await EmployeeData.GetData();
          }
          void Grid_UnboundColumnData(GridUnboundColumnDataEventArgs e) {
              if(e.FieldName == "FullName") {
                  e.Value = $"{e.GetRowValue("FirstName")} {e.GetRowValue("LastName")}";
              }
          }
      }
      

    Grid - Unbound Full Name Column