Skip to content

Commit

Permalink
DataGrid IsValid property added
Browse files Browse the repository at this point in the history
  • Loading branch information
enchev committed Nov 18, 2024
1 parent 5da0ff6 commit 5d5565b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
17 changes: 17 additions & 0 deletions Radzen.Blazor/RadzenDataGrid.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ namespace Radzen.Blazor
#endif
public partial class RadzenDataGrid<TItem> : PagedDataBoundComponent<TItem>
{
/// <summary>
/// Returns the validity of the DataGrid.
/// </summary>
/// <value><c>true</c> if all validators in the DataGrid a valid; otherwise, <c>false</c>.</value>
public bool IsValid
{
get
{
if (!editContexts.Any())
{
return true;
}

return editContexts.All(c => !c.Value.GetValidationMessages().Any());
}
}

/// <summary>
/// Gets or sets a value indicating whether this instance is virtualized.
/// </summary>
Expand Down
18 changes: 11 additions & 7 deletions RadzenBlazorDemos/Pages/DataGridInLineEdit.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@

<style>
.rz-grid-table {
width: unset;
width: unset;
}
</style>

<RadzenCard Variant="Variant.Outlined" class="rz-my-4">
<RadzenStack Orientation="Orientation.Horizontal" AlignItems="AlignItems.Center" Gap="0.5rem">
<div>Edit Mode:</div>
<RadzenSelectBar @bind-Value="@editMode" TextProperty="Text" ValueProperty="Value"
Data="@(Enum.GetValues(typeof(DataGridEditMode)).Cast<DataGridEditMode>().Select(t => new { Text = $"{t}", Value = t }))" Size="ButtonSize.Small"
Disabled="@(editMode == DataGridEditMode.Multiple && ordersToInsert.Count() > 1)" />
Data="@(Enum.GetValues(typeof(DataGridEditMode)).Cast<DataGridEditMode>().Select(t => new { Text = $"{t}", Value = t }))" Size="ButtonSize.Small"
Disabled="@(editMode == DataGridEditMode.Multiple && ordersToInsert.Count() > 1)" />
</RadzenStack>
</RadzenCard>

<RadzenDataGrid @ref="ordersGrid" AllowAlternatingRows="false" AllowFiltering="true" AllowPaging="true" PageSize="5" AllowSorting="true" EditMode="@editMode"
Data="@orders" TItem="Order" RowUpdate="@OnUpdateRow" RowCreate="@OnCreateRow" Sort="@Reset" Page="@Reset" Filter="@Reset" ColumnWidth="200px">
Data="@orders" TItem="Order" RowUpdate="@OnUpdateRow" RowCreate="@OnCreateRow" Sort="@Reset" Page="@Reset" Filter="@Reset" ColumnWidth="200px">
<HeaderTemplate>
<RadzenButton ButtonStyle="ButtonStyle.Success" Icon="add_circle" Text="Add New Order" Click="@InsertRow" Disabled="@(editMode == DataGridEditMode.Single && ordersToInsert.Count() > 0)" />
</HeaderTemplate>
Expand All @@ -29,7 +29,7 @@
<RadzenDataGridColumn Property="Customer.CompanyName" Title="Customer" Width="280px">
<EditTemplate Context="order">
<RadzenDropDown @bind-Value="order.CustomerID" Data="@customers" TextProperty="@nameof(Customer.CompanyName)" ValueProperty="@nameof(Customer.CustomerID)" Style="width:100%; display: block;"
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "Select customer" }})" />
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "Select customer" }})" />
</EditTemplate>
</RadzenDataGridColumn>
<RadzenDataGridColumn Property="Employee.LastName" Title="Employee" Width="220px">
Expand All @@ -39,7 +39,7 @@
</Template>
<EditTemplate Context="order">
<RadzenDropDown @bind-Value="order.EmployeeID" Data="@employees" ValueProperty="EmployeeID" Style="width:100%; display: block;"
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "Select employee" }})">
InputAttributes="@(new Dictionary<string,object>(){ { "aria-label", "Select employee" }})">
<Template>
<RadzenImage Path="@context.Photo" Style="width: 20px; height: 20px;" class="rz-border-radius-4 rz-me-2" />
@context.FirstName @context.LastName
Expand Down Expand Up @@ -135,7 +135,9 @@

async Task EditRow(Order order)
{
if (editMode == DataGridEditMode.Single && ordersToInsert.Count() > 0)
if (!ordersGrid.IsValid) return;

if (editMode == DataGridEditMode.Single)
{
Reset();
}
Expand Down Expand Up @@ -193,6 +195,8 @@

async Task InsertRow()
{
if (!ordersGrid.IsValid) return;

if (editMode == DataGridEditMode.Single)
{
Reset();
Expand Down

0 comments on commit 5d5565b

Please sign in to comment.