Skip to content
Manuel edited this page Aug 26, 2020 · 5 revisions

DataSources

The MGrid can handle two different data sources.

If you can, use the Property DataSource which is an IEnumerable. A common source could be the IQueryable of an Database Table i.e. nHibernate.

An alternative DataSource is the IMGridDataAdapter. It must be used in an Async Context, because DataSource does not support Async Operations. For Client Side Blazor there is an Adapter for an Odata Webservice: MComponents.Simple.Odata.Client

Simple example

<MGrid DataSource="Model">
    <MGridColumns>
        <MGridColumn Property="Name" HeaderText="Person" />
    </MGridColumns>
    <MGridPager />
</MGrid>

More complex example

<MGrid DataSource="Model"
           EnableEditing="mUserPermissions.CanEdit" EnableDeleting="mUserPermissions.CanEdit" EnableUserSorting="true" EnableFilterRow="true" EnableExport="true" EnableSaveState="true"
           ToolbarItems="ToolbarItem.Add">

        <MGridColumns>

            <MGridColumn Property=@nameof(WorkTime.StartTime) HeaderText="Start" />

            <MGridColumn Property=@($"{nameof(WorkTime.Tenure)}.{nameof(WorkTime.Tenure.Person)}.{nameof(WorkTime.Tenure.Person.Display)}") HeaderText="Person" />
 
            <MGridColumn Property=@nameof(WorkTime.HoursAtWork) HeaderText="Hours" Attributes="new[] { new ReadOnlyAttribute()}" EnableFilter="false" />

            <MGridComplexPropertyColumn Identifier="job" T="WorkTime" TProperty="Job" Property=@nameof(WorkTime.Job) >
                <FormTemplate Context="templContext">
                    <MSelect T="Job"
                             id=@templContext.InputId
                             class="form-control"
                             Property="@nameof(Job.Name)"
                             Options="session.Query<Job>().OrderBy(j => j.Name).ToArray()"
                             Value="templContext.Value"
                             ValueChanged="templContext.ValueChanged"
                             ValueExpression="templContext.ValueExpression"
                             NullValueDescription=@("\u200b") />
                </FormTemplate>
                <CellTemplate Context="cellContext">
                    @cellContext.Job?.Name
                </CellTemplate>
            </MGridComplexPropertyColumn>

            <MGridComplexPropertyColumn T="IMonitor" TProperty="MonitorRail" Property=@nameof(IMonitor.MonitorRail)
                                        Comparer="new NameableComparer()" EnableSorting="true"
                                        ReferencedPropertyToDisplay="@nameof(MonitorRail.Name)"
                                        ReferencedValues="mMonitorRails">
            </MGridComplexPropertyColumn>

            <MGridActionColumn T="WorkTime" class="table-content-fit" HeaderText="Aktionen" Identifier="Actions" />
        </MGridColumns>

        <MGridPager />

        <MGridEvents T=WorkTime OnBeginEdit="OnBeginEdit" OnBeginAdd="OnBeginAdd" OnAfterDelete="OnAfterDelete" />
    </MGrid>

Persist state

Set EnableSaveState="true" to persist the current page, filter, sorting and selected row to the local storage of the browser.

When you are using a MGridComplexPropertyColumn the ReferencedValues must be passed as a parameter, so the grid is able to retrieve the stored value. The Id of the referenced value will be stored in the browser.

Clone this wiki locally