Skip to content

Commit

Permalink
DropDownDataGrid binding to dynamic data demo added
Browse files Browse the repository at this point in the history
  • Loading branch information
enchev committed Apr 30, 2024
1 parent 94c19c8 commit 9f42ad5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
102 changes: 102 additions & 0 deletions RadzenBlazorDemos/Pages/DropDownDataGridDynamicData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
@inherits DbContextPage

<div class="rz-p-12 rz-text-align-center">
<RadzenLabel Text="Select Value" Component="DropDownDataGridBindValue" Style="margin-right: 8px; vertical-align: middle;" />
<RadzenDropDownDataGrid @ref=grid Data="@data" ColumnWidth="200px" TValue="IDictionary<string, object>"
AllowFiltering="true" AllowSorting="true" Value="@selectedItem" Change="@OnChange">
<ValueTemplate>
@string.Join(", ", columns.Where(c => c.Value == typeof(string)).Take(grid.MaxSelectedLabels).Select(c => context[c.Key]))
</ValueTemplate>
<Columns>
@foreach (var column in columns)
{
<RadzenDropDownDataGridColumn @key=@column.Key Title="@column.Key" Type="column.Value"
Property="@GetColumnPropertyExpression(column.Key, column.Value)">
<Template>
@context[@column.Key]
</Template>
</RadzenDropDownDataGridColumn>
}
</Columns>
</RadzenDropDownDataGrid>
</div>

@code {
RadzenDropDownDataGrid<IDictionary<string, object>> grid;
IDictionary<string, object> selectedItem;

public IEnumerable<IDictionary<string, object>> data { get; set; }

public IDictionary<string, Type> columns { get; set; }

public enum EnumTest
{
EnumValue1,
EnumValue2
}

void OnChange(object value)
{
selectedItem = (IDictionary<string, object>)value;
}

public string GetColumnPropertyExpression(string name, Type type)
{
var expression = $@"it[""{name}""].ToString()";

if (type == typeof(int))
{
return $"int.Parse({expression})";
}
else if (type == typeof(DateTime))
{
return $"DateTime.Parse({expression})";
}
else if (type.IsEnum)
{
return $@"Int32(Enum.Parse(it[""{name}""].GetType(), {expression}))";
}

return expression;
}

protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();

columns = new Dictionary<string, Type>()
{
{ "EmployeeID", typeof(int) },
{ "MyColumn", typeof(EnumTest) },
{ "FirstName", typeof(string) },
{ "LastName", typeof(string) },
{ "HireDate", typeof(DateTime) },
};

foreach (var i in Enumerable.Range(0, 50))
{
columns.Add($"Column{i}", typeof(string));
}

data = Enumerable.Range(0, 100).Select(i =>
{
var row = new Dictionary<string, object>();

foreach (var column in columns)
{
row.Add(
column.Key,
column.Value == typeof(EnumTest)
? (i % 2 == 0 ? EnumTest.EnumValue1 : EnumTest.EnumValue2)
: column.Value == typeof(int)
? i
: column.Value == typeof(DateTime)
? DateTime.Now.AddMonths(i)
: $"{column.Key}{i}"
);
}

return row;
});
}
}
8 changes: 8 additions & 0 deletions RadzenBlazorDemos/Pages/DropDownDataGridPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@
<DropDownDataGridDensity />
</RadzenExample>

<RadzenText Anchor="dropdown-datagrid#dynamic" TextStyle="TextStyle.H5" TagName="TagName.H2" class="rz-pt-12 rz-mb-6">
DropDownDataGrid binding to dynamic data
</RadzenText>
<RadzenExample ComponentName="DropDownDataGrid" Example="DropDownDataGridDynamicData">
<DropDownDataGridDynamicData />
</RadzenExample>



<RadzenText Anchor="dropdown-datagrid#keyboard-navigation" TextStyle="TextStyle.H5" TagName="TagName.H2" class="rz-pt-12">
Keyboard Navigation
Expand Down

0 comments on commit 9f42ad5

Please sign in to comment.