I have a DataGrid binded on this class:
public class JobPositionDTO : EntityChangesDTO
{
public EntityBaseDTO ProductType { get; set; }
public int ItemsCount { get; set; }
public int EquipmentsCount { get; set; }
public string Var1 {get; set; }
}
with EntityBaseDTO class defined as follow:
public class EntityBaseDTO
{
public string Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
I would like to show in the edit mode a drop down list with a list of all ProductType values but when I confirm the edit the value is always the old one.
Here my markup:
<RadzenDataGrid @ref="_dataGrid1" AllowMultiColumnSorting="true" AllowFiltering="true" AllowPaging="true" AllowSorting="true" AllowColumnResize="true"
FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.Simple" PageSize="@_pageSize" Data="@_data" LoadData="@LoadData" TItem="JobPositionDTO"
IsLoading="@_isLoading"
Count="@_count">
<Columns>
<RadzenDataGridColumn Width="Auto" TItem="JobPositionDTO" Property="ProductType.Code" Title="@Loc["ProductType"]" FilterOperator="FilterOperator.Equals"
Sortable="true" Filterable="true">
<EditTemplate Context="data">
<RadzenDropDown @bind-Value="@data.Element.ProductType.Id" Data="@productTypesL" TextProperty="Code" ValueProperty="Id" Style="width:100%" />
</EditTemplate>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="JobPositionDTO" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="100px">
<Template Context="data">
<RadzenButton Icon="edit" Size="ButtonSize.Small" Click="@(args => EditRow(data))" @onclick:stopPropagation="true" />
</Template>
<EditTemplate Context="data">
<RadzenButton Icon="save" Size="ButtonSize.Small" Click="@((args) => SaveRow(data))" />
<RadzenButton Icon="cancel" Size="ButtonSize.Small" ButtonStyle="ButtonStyle.Secondary" Click="@((args) => CancelEdit(data))" />
</EditTemplate>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
@code
{
private async Task SaveRow(JobPositionDTO jobPosition)
{
// jobPosition HAS ALWAYS OLD ProductType VALUE
}
private void CancelEdit(JobPositionDTO jobPosition)
{
_dataGrid1.CancelEditRow(jobPosition);
}
private void EditRow(JobPositionDTO jobPosition)
{
_dataGrid1.EditRow(jobPosition);
}
}
korchev
November 23, 2021, 9:37pm
2
I see a discrepancy. TItem of the DataGrid is set to JobPositionDTO
while TItem of the column is set to IViewModel< JobPositionDTO>
. This is not supported - both should be set to the same type.
Sorry, a copy and paste error.
It's always TItem="JobPositionDTO"
Problem is still valid.
Thank you
korchev
November 25, 2021, 8:18am
5
JobPositionDTO does not have an Element property considering the provided code.
Sorry, fixed code
public class DropDownItem
{
public string Text { get; set; }
public string Value { get; set; }
}
public class JobPositionDTO
{
public EntityBaseDTO ProductType { get; set; }
public int ItemsCount { get; set; }
public int EquipmentsCount { get; set; }
public string Id {get; set; }
}
public class EntityBaseDTO
{
public string Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
<RadzenDataGrid @ref="_dataGrid1" AllowMultiColumnSorting="true" AllowFiltering="true" AllowPaging="true" AllowSorting="true" AllowColumnResize="true"
FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" FilterMode="FilterMode.Simple"
PageSize="@_pageSize" Data="@_data" LoadData="@LoadData" TItem="JobPositionDTO"
IsLoading="@_isLoading"
Count="@_count">
<Columns>
<RadzenDataGridColumn Width="Auto" TItem="JobPositionDTO" Property="ProductType.Id" Title="@Loc["ProductType"]">
<EditTemplate Context="data">
<RadzenDropDown @bind-Value="@data.ProductType.Id" Data="@productTypes" TextProperty="Text" ValueProperty="Value" Style="width:100%" />
</EditTemplate>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="JobPositionDTO" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="100px">
<Template Context="data">
<RadzenButton Icon="edit" Size="ButtonSize.Small" Click="@(args => EditRow(data))" @onclick:stopPropagation="true" />
</Template>
<EditTemplate Context="data">
<RadzenButton Icon="save" Size="ButtonSize.Small" Click="@((args) => SaveRow(data))" />
<RadzenButton Icon="cancel" Size="ButtonSize.Small" ButtonStyle="ButtonStyle.Secondary" Click="@((args) => CancelEdit(data))" />
</EditTemplate>
</RadzenDataGridColumn>
</Columns>
</RadzenDataGrid>
@code
{
private RadzenDataGrid<JobPositionDTO> _dataGrid1;
private ICollection<JobPositionDTO> _data;
private int _count;
private bool _isLoading;
private int _pageSize = 50;
private string productType;
private List<DropDownItem> productTypes;
private async Task LoadData(LoadDataArgs args)
{
_data = new List<JobPositionDTO>{new JobPositionDTO {
ProductType = new EntityBaseDTO
{
Id= "1",
Code="Code1",
Name = "Name1"
},
ItemsCount = 5,
EquipmentsCount = 4,
Id = "Id1"
},
new JobPositionDTO {
ProductType = new EntityBaseDTO
{
Id= "2",
Code="Code2",
Name = "Name2"
},
ItemsCount = 23,
EquipmentsCount = 14,
Id = "Id2"
},
});
// It's a collection created on ProductType all values: DropDownItem Value match with EntityBaseDTO Id
productTypes = new List<DropDownItem>{
new DropDownItem
{
Value= "1",
Text="Name1 - Code1"
},
new DropDownItem
{
Value= "2",
Text="Name2 - Code2"
},
new DropDownItem
{
Value= "3",
Text="Name3 - Code3"
},
});
}
private async Task SaveRow(JobPositionDTO jobPosition)
{
// jobPosition HAS ALWAYS OLD ProductType VALUE
}
private void CancelEdit(JobPositionDTO jobPosition)
{
_dataGrid1.CancelEditRow(jobPosition);
}
private void EditRow(JobPositionDTO jobPosition)
{
_dataGrid1.EditRow(jobPosition);
}
}
korchev
November 25, 2021, 11:36am
7
Try setting ProductType itself.
<RadzenDropDown @bind-Value="@data.ProductType"
Data="@productTypes" TextProperty="Text" Style="width:100%" />
Exception: cannot convert from DropDownItem to EntityBaseDTO.
It works if I do:
<RadzenDropDown @bind-Value="@data.ProductType" Data='@_productTypesL' TextProperty="Code" Style="width:100%"></RadzenDropDown>
where _productTypesL is a List<EntityBaseDTO>
, but in this way I can't format my text value to "Code - Name"
Solved in this way:
<RadzenDropDown @bind-Value="@data.Element.ProductType" Data='@_productTypesL' TextProperty="Code" Style="width:100%">
<Template Context="item">
@($"{((EntityBaseDTO)item).Code} - {((EntityBaseDTO)item).Name}")
</Template>
</RadzenDropDown>
Thank you very much
Hola tengo un problema similar. En tipo me trae PrototipoI.Shared.Models...
Este es mi código:
<RadzenDataGridColumn TItem="SolLicencia" Property="TipoLicencia.Nombre" Title="Tipo" Width="280px" >
<EditTemplate Context="data">
<RadzenDropDown @bind-Value="@data.TipoLicencia.TipoLicenciaID" Data="@tiposLicencias" TextProperty="Nombre" ValueProperty="TipoLicenciaID" Style="width:100%; display: block;" >
<Template Context="item">
@($"{((TipoLicencia)item).Nombre} - {((TipoLicencia)item).TipoLicenciaID}")
</Template>
</RadzenDropDown>
</EditTemplate>
</RadzenDataGridColumn>
Modelos:
public class SolLicencia
{
public int SolLicenciaId { get; set; }
public int IdLegajo { get; set; }
public DateTime FechaSolicitud { get; set; } = DateTime.Now;
public TipoLicencia TipoLicencia { get; set; }
public virtual DateTime FechaDesde { get; set; } = DateTime.Now;
public virtual DateTime FechaHasta { get; set; } = DateTime.Now;
public virtual int TotalDias { get; set; } = 1;
public string Estado { get; set; } = "Pendiente";
public string Observaciones { get; set; }
public string ObservacionesManager { get; set; }
public string ObservacionesAdmin { get; set; }
}
public class TipoLicencia
{
public int TipoLicenciaID { get; set; }
public string Nombre { get; set; }
}
}
Espero puedan ayudarme. Soy nueva en blazor.