Skip to content

Commit

Permalink
Added Save/Load settings support for DataGrid columns without Property
Browse files Browse the repository at this point in the history
Fix #1058
  • Loading branch information
enchev committed Aug 7, 2023
1 parent 32525a0 commit be1a6cc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Radzen.Blazor/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public class DataGridSettings
/// </summary>
public class DataGridColumnSettings
{
/// <summary>
/// Property.
/// </summary>
public string UniqueID { get; set; }

/// <summary>
/// Property.
/// </summary>
Expand Down
9 changes: 6 additions & 3 deletions Radzen.Blazor/RadzenDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2871,8 +2871,9 @@ internal void SaveSettings()
{
settings = new DataGridSettings()
{
Columns = ColumnsCollection.ToList().Where(c => !string.IsNullOrEmpty(c.Property)).Select(c => new DataGridColumnSettings()
Columns = ColumnsCollection.ToList().Select(c => new DataGridColumnSettings()
{
UniqueID = c.UniqueID,
Property = c.Property,
Width = c.GetWidth(),
Visible = c.GetVisible(),
Expand Down Expand Up @@ -2913,7 +2914,8 @@ internal async Task LoadSettings(DataGridSettings settings)
{
foreach (var column in settings.Columns.OrderBy(c => c.SortIndex))
{
var gridColumn = ColumnsCollection.Where(c => c.Property == column.Property).FirstOrDefault();
var gridColumn = ColumnsCollection.Where(c => c.Property == column.Property).FirstOrDefault() ??
ColumnsCollection.Where(c => c.UniqueID == column.UniqueID).FirstOrDefault();
if (gridColumn != null)
{
// Sorting
Expand All @@ -2927,7 +2929,8 @@ internal async Task LoadSettings(DataGridSettings settings)

foreach (var column in settings.Columns)
{
var gridColumn = ColumnsCollection.Where(c => c.Property == column.Property).FirstOrDefault();
var gridColumn = ColumnsCollection.Where(c => c.Property == column.Property).FirstOrDefault() ??
ColumnsCollection.Where(c => c.UniqueID == column.UniqueID).FirstOrDefault();
if (gridColumn != null)
{
// Visibility
Expand Down
8 changes: 8 additions & 0 deletions Radzen.Blazor/RadzenDataGridColumn.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,19 @@ internal int GetRowSpan(bool isDataCell = false)
Type _propertyType;
internal Type PropertyType => _propertyType;

/// <summary>
/// Gets or sets the unique identifier.
/// </summary>
/// <value>The unique identifier.</value>
public string UniqueID { get; set; }

/// <summary>
/// Called when initialized.
/// </summary>
protected override void OnInitialized()
{
UniqueID = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Replace("/", "-").Replace("+", "-").Substring(0, 10);

if (Grid != null)
{
Grid.AddColumn(this);
Expand Down
2 changes: 1 addition & 1 deletion RadzenBlazorDemos/Pages/DataGridSaveSettings.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
AllowColumnResize="true" AllowColumnReorder="true" ColumnWidth="200px"
FilterPopupRenderMode="PopupRenderMode.OnDemand" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive" Data="@employees" TItem="Employee">
<Columns>
<RadzenDataGridColumn TItem="Employee" Property="Photo" Title="Employee" Sortable="false" Filterable="false">
<RadzenDataGridColumn TItem="Employee" Title="Employee" Sortable="false" Filterable="false">
<Template Context="data">
<RadzenImage Path="@data.Photo" style="width: 40px; height: 40px; border-radius: 8px; margin-right: 8px;" />
@data.FirstName @data.LastName
Expand Down

0 comments on commit be1a6cc

Please sign in to comment.