admin管理员组

文章数量:1122832

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.

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

Share Improve this question edited Nov 22, 2024 at 2:48 Qiang Fu 8,0731 gold badge6 silver badges16 bronze badges asked Nov 21, 2024 at 18:40 HomerHomer 7,80614 gold badges71 silver badges111 bronze badges 3
  • Do you mean you don't want the TemplateColumn to be editable? – RBee Commented Nov 21, 2024 at 19:03
  • I do want it editable. I'm looking for it to be a textbox when the row is in edit mode and colored text when it is not. – Homer Commented Nov 21, 2024 at 19:35
  • You already have that in your snippet though. Can you elaborate on what you're looking for. – RBee Commented Nov 21, 2024 at 22:27
Add a comment  | 

1 Answer 1

Reset to default 1

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".

本文标签: