Search code examples
asp.net-coreblazormudblazor

In a MudBlazor DataGrid, how can I use inline editing and also use TemplateColumn?


I'm using a MudBlazor DataGrid with cells that use TemplateColumn for custom color. I would like to use EditMode="DataGridEditMode.Cell" for inline editing, but when I do I lose the TemplateColumn.

Is there a way to turn on inline editing only for the selected row? Using DataGridEditTrigger.OnRowClick" does not work.

https://try.mudblazor.com/snippet/wYcIPPcvKUrbAtTK


Solution

  • I think you could use PropertyColumn instead of TemplateColumn to make cell inline editing work.

    <MudDataGrid Items="@Tests" T="Test" ReadOnly="false" EditMode="DataGridEditMode.Cell" >
        <Columns>
            <PropertyColumn Property="x => x.Id" Title="Id" />
            <PropertyColumn Property="x=>x.Compliant" Title="Compliant"/>
            <PropertyColumn Property="x=>x" Title="Compliant Color">
                <EditTemplate>
                    <MudTextField Style="@(context.Item.Compliant.StartsWith("N")?"color: red":"")" @bind-Value="context.Item.Compliant"></MudTextField>
                </EditTemplate>
            </PropertyColumn>
        </Columns>
    </MudDataGrid>
    
    @code {
        private List<Test> Tests = new()
        {
            new Test { Id = 1, Compliant = "Yes"},
            new Test { Id = 2, Compliant = "No" }
        };
    
        public class Test
        {
            public int Id { get; set; }
            public string Compliant { get; set; }
        }
    }
    

    When you modify the "PropertyColumn" by "EditTemplate", the default template is removed. So it doesn't even matter what the "Property" is, just use Property="x=>x".