# Blazor DataTable Wrapper: DataControl

URL: https://flexcoreui.com/demo/data-control  
Section: Data Grid  
Controls: DataControl, GridControl, ChartControl  
Keywords: DataTable, DataFrame, in-memory table, data binding

Shape data once and reuse it: DataControl wraps a .NET DataTable in a typed API to filter and sort, then feeds a Blazor grid, a chart and a CSV.

## Documentation

### Props

| Name | Type | Default | Description |
|---|---|---|---|
| Table | DataTable |  | The underlying DataTable. Assigning a new one clears the selection and reapplies Filter and Sort. |
| Filter | string? | null | DataView row filter expression applied to the current view. |
| Sort | string? | null | DataView sort expression applied to the current view. |
| Shape | DataControlShape |  | Rows in the current view and columns. |
| Size | int |  | Rows × columns of the current view. |
| Empty | bool |  | True when the view has no rows or the table no columns. |
| Columns | IReadOnlyList<DataControlColumn> |  | Name and type of each column. |
| Dtypes | IReadOnlyDictionary<string, Type> |  | Column name to CLR type. |
| RowCount / TotalRowCount | int |  | Rows in the current view; rows in the table. |
| IsDirty | bool |  | True while the table has uncommitted changes. |
| At / Iat | DataControlAtIndexer / DataControlIatIndexer |  | Cell by row label and column name; cell by 0-based row and column position. Both read and write. |

### Methods

| Name | Type | Description |
|---|---|---|
| DataFrame(data, index, columns, dtype, copy) | DataControl | Static. Builds a frame from column arrays keyed by name, with optional row labels, column order and a forced type. |
| FromRows(rows, tableName) | DataControl | Static. Builds a frame from any sequence of objects. |
| FromDataTable(table) | DataControl | Static. Wraps an existing table. |
| ConvertDtypes() | DataControl | New frame with object and string columns converted to the type every value parses as. |
| InferObjects() | DataControl | The same for object columns only. |
| Astype(columnName, dtype) | DataControl | New frame with one column forced to a type. |
| Query(expression) | DataControl | New frame holding the rows a DataTable Select expression matches. |
| SortBy(columnName, descending) | DataControl | Sets Sort on this frame and returns it. |
| GroupBy(groupColumn, valueColumn, aggregate) | IReadOnlyList<DataControlAggregateResult> | One result per distinct group value over the current view: the aggregate of valueColumn and the row count. |
| Find(expression, startRow, endRow) | int | 1-based number of the first row in the range matching the expression; 0 when none. |
| ToGridRows() | IEnumerable<ExpandoObject> | Current view as ExpandoObject rows, one key per column. |
| ToChartBars(labelColumn, valueColumn, colorColumn) | List<ChartControl.ChartBar> | One bar per row of the current view; the optional color column names each bar's color. |
| ToChartSeries(labelColumn, valueColumns) | List<ChartSeries> | One series per value column, with a point per row of the current view. |
| ToReportDefinition(title) | ReportDefinition | A report definition whose columns mirror the table: header from the column caption, numeric columns right-aligned. |
| ToCsv() / ToJson(options) / SaveAs(format) | string | The current view as text; SaveAs picks Csv or Json. |

### Events

| Name | Type | Description |
|---|---|---|
| DataChanged | EventHandler | After the table, filter, sort or a cell changed, or changes were reset or rejected. |
| RowChanged | EventHandler<DataControlRowChangedEventArgs> | After one row changed, with its 1-based number and the DataRow. |

## Examples

### Frame

```razor
@using System.Dynamic
@using Fx.ControlKit
@using Fx.ControlKit.Charts
@using Fx.ControlKit.Grid

<TextBoxControl @bind-Value="_filterText" Placeholder="Amount > 1000" Width="220px" />
<ButtonControl CssClass="btn-sm btn-primary" OnClick="ApplyFilter">Apply filter</ButtonControl>
<ButtonGroupControl Items="_sortItems" Values="@(new[] { _sortKey })" ValuesChanged="PickSort" Selection="ChoiceSelectionMode.Single" AriaLabel="Sort" />

<div class="views">
    <GridControl TValue="ExpandoObject" DataSource="@_rows" Height="260px" AllowSorting="false" ShowRowCountInFooter="true">
        <GridColumnsBase>
            @foreach (var column in _frame.Columns)
            {
                var type = _frame.Dtypes[column.Name];
                <GridColumn @key="column.Name" Field="@column.Name" HeaderText="@column.Name"
                            Type="@(type == typeof(bool) ? ColumnType.Boolean : type == typeof(DateTime) ? ColumnType.Date : type == typeof(decimal) || type == typeof(int) || type == typeof(long) ? ColumnType.Number : ColumnType.Text)"
                            Format="@(type == typeof(decimal) ? "N2" : type == typeof(DateTime) ? "d" : null)"
                            TextAlign="@(type == typeof(decimal) || type == typeof(int) || type == typeof(long) ? TextAlign.Right : TextAlign.Left)" />
            }
        </GridColumnsBase>
    </GridControl>
    <ChartControl Type="ChartType.Bar" ColorPalette="@ChartPalette.Site" Title="Amount by order" Series="@_series" YAxis="_amountAxis" Height="232px" />
</div>
<pre class="csv">@_csv</pre>
<p class="result @(_error is null ? null : "bad")">@Status</p>
```

