Back to Devexpress

DxGrid.Columns Property

blazor-devexpress-dot-blazor-dot-dxgrid-212f140c.md

latest6.6 KB
Original Source

DxGrid.Columns Property

Allows you to add Grid columns.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
[Parameter]
public RenderFragment Columns { get; set; }

Property Value

TypeDescription
RenderFragment

A template (render fragment) that allows you to declare Grid columns.

|

Remarks

The Grid supports the following column types:

Bound Data Columns

These columns get their values from the bound data source. Use the Data property to bind the Grid to a data collection. Declare DxGridDataColumn objects in the Columns template and use each object’s FieldName property to assign data fields. Note that the FieldName property value must be unique for each data column.

For additional information, refer to the following topic: Create a Bound Column.

Unbound Data Columns

These columns display values that are not stored in the assigned data collection. To create an unbound column, declare a DxGridDataColumn object in the Columns template and specify the column’s UnboundType property. Then, define the UnboundExpression property or handle the UnboundColumnData event to calculate column values.

For additional information, refer to the following topic: Create an Unbound Column.

Command Column

This column displays CRUD-related buttons ( New , Edit , and Delete ) and the Clear button that resets values in the filter row. To create this column, declare a DxGridCommandColumn object in the Columns template.

For additional information, refer to the DxGridCommandColumn class description.

Selection Column

This column allows users to select and deselect rows. It contains checkboxes or radio buttons depending on selection mode. To create this column, declare a DxGridSelectionColumn object in the Columns template.

For additional information, refer to the DxGridSelectionColumn class description.

The following example creates data columns and binds them to data fields from the assigned data collection:

razor
@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();
    }
}
csharp
using System;

public class WeatherForecast {
    public DateTime Date { get; set; }
    public int TemperatureC { get; set; }
    public double TemperatureF => Math.Round((TemperatureC * 1.8 + 32), 2);
    public string Forecast { get; set; }
    public string CloudCover { get; set; }
    public bool Precipitation { get; set; }
}
csharp
using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;

    public class WeatherForecastService {
        private List<WeatherForecast> Forecast { get; set; }

        private static string[] CloudCover = new[] {
            "Sunny", "Partly cloudy", "Cloudy", "Storm"
        };

        Tuple<int, string>[] ConditionsForForecast = new Tuple<int, string>[] {
            Tuple.Create( 22 , "Hot"),
            Tuple.Create( 13 , "Warm"),
            Tuple.Create( 0 , "Cold"),
            Tuple.Create( -10 , "Freezing")
        };

        public WeatherForecastService() {
            Forecast = CreateForecast();
        }

        private List<WeatherForecast> CreateForecast() {
            var rng = new Random();
            DateTime startDate = DateTime.Now;
            return Enumerable.Range(1, 15).Select(index => {
                var temperatureC = rng.Next(-10, 30);
                return new WeatherForecast {
                    Date = startDate.AddDays(index),               
                    TemperatureC = temperatureC,
                    CloudCover = CloudCover[rng.Next(0, 4)],
                    Precipitation = Convert.ToBoolean(rng.Next(0, 2)),
                    Forecast = ConditionsForForecast.First(c => c.Item1 <= temperatureC).Item2
                };
            }).ToList();
        }

        public IEnumerable<WeatherForecast> GetForecast() {
            return Forecast.ToArray();
        }
        // ...
}
csharp
// ...
builder.Services.AddSingleton<WeatherForecastService>();

Run Demo: Data Binding Run Demo: Edit Data Run Demo: Selection Column View Example: Create a Foreign Key (ComboBox) Column

See Also

DxGrid Class

DxGrid Members

DevExpress.Blazor Namespace