Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Only datagrid with Dictionary #3278

Closed
Browniee97 opened this issue Jan 2, 2022 · 2 comments
Closed

Read Only datagrid with Dictionary #3278

Browniee97 opened this issue Jan 2, 2022 · 2 comments

Comments

@Browniee97
Copy link

I am trying to load a dynamic CSV and display with a datagrid. I understand that there are complexities when trying to properly bind a dynamic object to datatable however I do not need the data to have a bi-directional binding as I only require the table to be readonly. using the below code I am able to get the datagrid to render properly with all the data before it throws an error.

@inject IJSRuntime JS

<InputFile id="fileUpload" class="form-control-file" OnChange="@LoadFile" />
@if (data.Count > 0)
{
    <DataGrid TItem="Dictionary<string,object>"
              Data="@data"
              Filterable="false"
              Editable="false"
              FixedHeader="true"
              FixedHeaderDataGridHeight="85vh"
              Virtualize="true"
              Resizable="true"
              ResizeMode="TableResizeMode.Header"
              Hoverable="true"
              Bordered="true"
              Striped="true">

        <DataGridColumns>
            @foreach (var header in headers)
            {
                <DataGridColumn TItem="Dictionary<string,object>" Field="@header" Caption="@header" VerticalAlignment="VerticalAlignment.Middle">
                    <DisplayTemplate>
                        @(context[header].ToString())
                    </DisplayTemplate>
                </DataGridColumn>
            }
        </DataGridColumns>
    </DataGrid>
}



@code {

    List<string> headers = new();
    List<Dictionary<string, object>> data = new();

    private async void LoadFile(InputFileChangeEventArgs e)
    {
        //When the file is loaded it is checked that it is the correct type
        var temp = e.File.Name.Substring(e.File.Name.LastIndexOf('.'));
        if (await JS.InvokeAsync<bool>("checkFile", "fileUpload", ".csv"))
        {
            data = (await Helpers.FileHelper.processCsv(e.File)).ToDictionary();

            //A record of the column headers from the uploaded data is kept, this will be used to map those columns to their corresponding salesforce fields
            headers = data.First().Keys.ToList();

            StateHasChanged();
        }
    }
}

This loads and renders all the data properly before throwing the following error.

Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
      Unhandled exception in circuit 'FXZa4UjQaGse8MkeIGHHI0_D7fRzDymDtgULLpXL2Y0'.
      System.AggregateException: One or more errors occurred. (Cannot detect the member of System.Collections.Generic.Dictionary`2[System.String,System.Object] (Parameter 'id'))
       ---> System.ArgumentException: Cannot detect the member of System.Collections.Generic.Dictionary`2[System.String,System.Object] (Parameter 'id')
         at Blazorise.DataGrid.Utils.FunctionCompiler.GetSafePropertyOrField(Expression item, String propertyOrFieldName)
         at Blazorise.DataGrid.Utils.FunctionCompiler.CreateValueGetter[TItem](String fieldName)
         at Blazorise.DataGrid.DataGridColumn`1.<.ctor>b__8_2()
         at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
         at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
         at System.Lazy`1.CreateValue()
         at Blazorise.DataGrid._BaseDataGridRow`1.OnAfterRenderAsync(Boolean firstRender)
         at Microsoft.AspNetCore.Components.Rendering.ComponentState.NotifyRenderCompletedAsync()
@David-Moreira David-Moreira self-assigned this Jan 9, 2022
@David-Moreira
Copy link
Contributor

David-Moreira commented Jan 31, 2022

Hello @Browniee97
We've made it so Field is no longer mandatory. If you do not need that DataGrid handles binding automatically for you, you can just remove it.

In your example, it should work by just removing it.

image
https://blazorise.com/news/release-notes/095

Edit: Also, please apologize us for the late reply. :)

@David-Moreira
Copy link
Contributor

My guess is that the new "Non Mandatory Field Parameter" feature should solve the OP's issue. Closing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants