akoustov / Grid_sample

0 stars 0 forks source link

Using Grid inline, could not add anew record to the list #1

Open akoustov opened 3 years ago

akoustov commented 3 years ago

Hi guys, I have a blazor app .net core 3.1 where I took as a base the demo- DataGrid InLine Editing (https://blazor.radzen.com/datagrid-inline-edit) to create a grid. From my DB I can load data to the grid, but could not add a new record. My grid has only 2 items: pulldown menu and text box. When I add a record, the record is not going to the list, also edit-save does not work, I have nuget Radzen.Blazor(3.1.5). Here is the code.


@page "/bhr"

@using System @using BhrTools.Data @using Microsoft.EntityFrameworkCore @inject BHRecordService ser

<AuthorizeView>
   <b>   Hello, @context.User.Identity.Name! </b>
</AuthorizeView>
<br>
<br/>

    <table bRecord2="1" cellpadding="10" class="center">
    <tr>
            <th style="width:300px">Total URG</th>  <th style="width:500px">Total Future CC/ACH Pay Amt</th>   <th style="width:300px">Total Promises</th>          
    </tr>
     @foreach (TotalBHR total in @totals)
     {
     <tr> <td style="width:300px">@total.Total_URG </td> <td style="width:500px">@total.Total_Future </td> <td style="width:300px">@total.Total_Promise</td>                        
     </tr>
     }
</table>


<RadzenButton Icon="add_circle_outline" style="margin-bottom: 10px; color: darkblue; vertical-align: middle;" Text="Create New" Click="@InsertRow" ButtonStyle="ButtonStyle.Light"/>
<RadzenGrid @ref="recordsGrid" AllowFiltering="true" AllowPaging="true" PageSize="10" AllowSorting="true" EditMode="DataGridEditMode.Single"
            Data="@records" TItem="Record2" RowUpdate="@OnUpdateRow" RowCreate="@OnCreateRow">
    <Columns>

        <RadzenGridColumn Width="200px" TItem="Record2" Property="Record2.Desk" Title="Desk">
            <EditTemplate Context="record">
                <RadzenDropDown @bind-Value="record.Desk" Data="@desks" TextProperty="DisplayName" ValueProperty="DisplayName" Style="width:100%" />
            </EditTemplate>
        </RadzenGridColumn>

        <RadzenGridColumn TItem="Record2" Property="Account" Title="Account">
            <EditTemplate Context="record">
                <RadzenTextBox @bind-Value="record.Account" Style="width:100%; display: block" Name="ShipName" />
                <RadzenRequiredValidator Text="Account is required" Component="Account" Popup="true" />
            </EditTemplate>
        </RadzenGridColumn>

        <RadzenGridColumn TItem="Record2" Context="sampleBlazorModelsSampleRecord2" Bubble="false" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="100px">
            <Template Context="record">
                <RadzenButton Icon="edit" Size="ButtonSize.Small" Click="@(args => EditRow(record))">
                </RadzenButton>
            </Template>
            <EditTemplate Context="record">
                <RadzenButton Icon="save" Size="ButtonSize.Small" Click="@((args) => SaveRow(record))">
                </RadzenButton>
                <RadzenButton Icon="cancel" Size="ButtonSize.Small" ButtonStyle="ButtonStyle.Secondary" Click="@((args) => CancelEdit(record))">
                </RadzenButton>
            </EditTemplate>
        </RadzenGridColumn>

        <RadzenGridColumn TItem="Record2" Context="Record2" Bubble="false" Filterable="false" Sortable="false" TextAlign="TextAlign.Center" Width="70px">
            <Template Context="record">
                <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="close" Size="ButtonSize.Small" Click="@(args => DeleteRow(record))">
                </RadzenButton>
            </Template>
            <EditTemplate Context="record">
                <RadzenButton ButtonStyle="ButtonStyle.Danger" Icon="close" Size="ButtonSize.Small" Click="@(args => DeleteRow(record))">
                </RadzenButton>
            </EditTemplate>
        </RadzenGridColumn>
    </Columns>
</RadzenGrid>

@code { RadzenGrid recordsGrid; IList records; IEnumerable totals; IEnumerable pmmts; IEnumerable urgs; IEnumerable campaigns; IEnumerable clients; IEnumerable desks; string DayValue; string value; bool gbGate; DateTime? valueDT = DateTime.Now;

void OnChange(DateTime? value, string name, string format, string test)
{
    string str = value?.ToString(format);
}

void OnChange(string DayValue, string name, string format)
{
    int x = 0;
}

void OnChange(object value, string name)
{
    var str = value is IEnumerable<object> ? string.Join(", ", (IEnumerable<object>)value) : value;

}

protected override void OnInitialized()
{
    base.OnInitialized();

    gbGate = false;
    records = ser.GetRecords2();
    totals = ser.GetTotalList();
    desks = ser.GetDeskTypes();
    DayValue = GetDate();
    value = GetDate();
}

private string  GetDate()
{
    DateTime localDate = DateTime.Now;
    return localDate.Year.ToString() + "-" + localDate.Month.ToString() + "-" + localDate.Day.ToString();
}
void EditRow(Record2 record)
{
    recordsGrid.EditRow(record);
}

void SaveRow(Record2 record)
{
    int x = 0;

// Record2 record2 = new Record2("3", "123"); recordsGrid.UpdateRow(record); }

void OnUpdateRow(Record2 record)
{
    int x = 0;
//    dbContext.Update(record);

    // For demo purposes only

// Record2.Customer = dbContext.Customers.Find(Record2.CustomerID); // Record2.Employee = dbContext.Employees.Find(Record2.EmployeeID);

    // For production
    //dbContext.SaveChanges();
}

void CancelEdit(Record2 record)
{
    recordsGrid.CancelEditRow(record);

    // For production

// var recordEntry = dbContext.Entry(record); // if (recordEntry.State == EntityState.Modified) // { // recordEntry.CurrentValues.SetValues(recordEntry.OriginalValues); // recordEntry.State = EntityState.Unchanged; // } }

void DeleteRow(Record2 record)
{
    if (records.Contains(record))
    {

// dbContext.Remove(record);

        // For demo purposes only
        records.Remove(record);

        // For production
        //dbContext.SaveChanges();

        recordsGrid.Reload();
    }
    else
    {
        recordsGrid.CancelEditRow(record);
    }
}

void InsertRow()
{
    recordsGrid.InsertRow(new Record2());
}

void OnCreateRow(Record2 Record2)
{
    int x = 0;

// dbContext.Add(Record2);

    // For demo purposes only

// Record2.Customer = dbContext.Customers.Find(Record2.CustomerID); // Record2.Employee = dbContext.Employees.Find(Record2.EmployeeID);

    // For production
    //dbContext.SaveChanges();
}

}

akoustov commented 3 years ago

fixed a small problems in my code, found a way to add a record to the grid with
records.Add(record); recordsGrid.UpdateRow(record); recordsGrid.Reload();

but sorting in the headers does not work now

akoustov commented 3 years ago

after fixing my small problems I can add/delete a record in the grid. right now i have a double entry problem. it starts where in two columns I have only one entry, the second makes a req.validator error. After making the second entry this code(SaveRow() ) runs again and creates the same 2 rows in the grid. If a record is created without a reg.validator error I have only one entry.

void SaveRow(Record2 record) { records.Insert(0, record); //adds the item at front recordsGrid.UpdateRow(record); recordsGrid.Reload(); }

akoustov commented 3 years ago

very bad that I have to make this code to avoid double entry when a req.validator fires, do you have better way? void SaveRow(Record2 record) {
records.Insert(0, record); //adds the item at front recordsGrid.UpdateRow(record); recordsGrid.Reload(); if (record.CheckItems()) //if even one item in the object is null return false { records.RemoveAt(0); //adds the item at front recordsGrid.UpdateRow(record); recordsGrid.Reload(); } }

akoustov commented 3 years ago

I solved the problem. comments in your code should be entered even simple like this.

//called when the record is submitted and if not all data is entered calls validators after UpdateRow( ) void SaveRow(Record2 record) {
recordsGrid.UpdateRow(record);
}

//called when the record is submitted and all data is entered void OnCreateRow(Record2 record) { records.Insert(0, record); //adds the item at front recordsGrid.UpdateRow(record); recordsGrid.Reload();

    // For production
    //dbContext.SaveChanges();
}