GI1
GI1

Reputation: 126

Blazor(ise) DataGrid iterate through DataGridColumns in code (behind)

Sorry if this is a Blazorise and not just a general Blazor WebAseembly question, being a noob in both I'm just not sure so posting with both tags, but I'd like to display a list of items in a DataGrid that has certain columns sortable (multiple simultaneously and not by just one at a time), and once that sortable columns desc/asc/none direction setting occurs I'd like to be able to read their set direction properties state on a button click to pass it along to another component...only I can't do it with a @ref like this:

<DataGrid TItem="Item"
          Data="@ItemList"
          @ref="dataGrid"
          @bind-SelectedRow="@selectedItem"
          Responsive ShowPager="true" PageSize="10">
        <DataGridCommandColumn TItem="Item" />
        <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" Sortable="false" />
        <DataGridColumn TItem="Item" Field="@nameof(Item.SKU)" Caption="SKU" Sortable="false"/>
        <DataGridColumn TItem="Item" Field="@nameof(Item.ModelId)" Caption="Model" Sortable="false" />
        <DataGridColumn TItem="Item" Field="@nameof(Item.Year)" Caption="Year" Editable="true" Direction="SortDirection.Ascending" />
        <DataGridColumn TItem="Item" Field="@nameof(Item.Weight)" Caption="Weight" Editable="true" Direction="SortDirection.Ascending" />
</DataGrid>

@code {
    private Blazorise.DataGrid.DataGrid<Ad> dataGrid;
    protected void OnClick(MouseEventArgs mouseEventArgs)
    {
        int i = 0;
        string sortdirections="";
        dataGrid.DataGridColumns.
        foreach(Blazorise.DataGrid.DataGridColumn<Ad> col in dataGrid.DataGridColumns)
        {
            if (col.Sortable)
            {
                i++;
                switch(col.SortDirection){
                  case SortDirection.Ascending: smartsort += ("a" + i.ToString()); break;
                  case SortDirection.Descending: smartsort += ("d" + i.ToString()); break;
                  case SortDirection.None: break;
                }
            }
        }
        . . .
    }

Because I get a build error

CS1579 foreach statement cannot operate on variables of type 'RenderFragment' because 'RenderFragment' does not contain a public instance or extension definition for 'GetEnumerator'

given that the dataGrid.DataGridColumns seemingly doesn't reference the actual rendered instance of the dataGrid component I'm using (but a RenderFragment instead), because one should probably databind variables to those Direction properties I guess...but the trouble with me databinding Direction properties to some or particular variable(s) is that I'd like to make the DataGrid a bit more dynamic in the future, in which case I wouldn't even know in advance during design-time what and which columns would be sortable and the user could be setting some sortable columns to sort direction none...thus I need some (pardon the pun) direction please on how to do it this way (basically like using old ASP.NET server-side components to access its run-time state in a blazor webassembly app) or how to do it with preferably just a single databound variable dynamically but so that just a few columns out of the ones displayed are going to actually be sortable through a click on a datagrid column name with which the user could change the sort order direction (so that the databound variable then after the user finishes clicking the sort orders of colums holds the state of that datagrid indicating which of the sortable columns have been set to ascend and which of the to descend, if any of them, since they can also choose direction none)?

TIA

Upvotes: 1

Views: 1178

Answers (1)

Steve Greene
Steve Greene

Reputation: 12314

The latest version of Blazorise has a sort mode:

<DataGrid ... Sortable="true" SortMode="DataGridSortMode.Multiple" ...

https://blazorise.com/docs/helpers/enums/datagrid

If that's not exactly what you are looking for, ask Mladen on gitter or check out the source code here.

Upvotes: 1

Related Questions