This does solve he problem and I can get my DataTable to display in the Blazor Grid. But editing and saving back to the database seems to be a complex matter.
Why can't the Blazor SFGrid just use a DataTable as a DataSource like all other Data Grids?
Hi Mark,
Based on your query, we suggest using a custom adapter. We have already
documented this topic. Please see the documentation link below.
https://blazor.syncfusion.com/documentation/datagrid/custom-binding#custom-adaptor-as-component
https://blazor.syncfusion.com/documentation/datagrid/data-binding#sql-server-data-bindingsql-client
Regards,
Prathap S
Heloo syncfusion teams
It is possible to use datatable with Template column if yes could you help me please
Thanks
Hi Ahmad,
We would like to clarify that we don’t have direct support to bind data to the
DataGrid from a datatable, so it's not possible to use the datatable with a
template column. Please note that our Grid component requires strongly typed
objects or POCO object-type data for binding. This means that data binding must
be based on a specific model type (class). This behavior is inherent to the
default functionality of the grid. If the model type is unknown, we recommend
using ExpandoObjectBinding or DynamicObjectBinding. For more information on
these binding options, please refer to the following documentation link:
https://blazor.syncfusion.com/documentation/datagrid/columns#expandoobject-complex-data-binding
https://blazor.syncfusion.com/documentation/datagrid/columns#dynamicobject-complex-data-binding
However, you can achieve your requirement by converting the datatable to an
ExpandoObject list, allowing you to use the template. We have attached a sample
for your reference and code snippet for your reference.
<SfGrid DataSource="@CustomerList" TValue="ExpandoObject" AllowPaging="true" AllowSorting="true" AllowFiltering="true" AllowGrouping="true"> <GridPageSettings PageSize="5"></GridPageSettings> <GridColumns> <GridColumn Field="OrderID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn> <GridColumn Field="CustomerID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"> <Template> @{ var employee = (context as IDictionary<string, object>); var edit = (string)employee["CustomerID"];
<p>CustomerName:@edit</p> } </Template> </GridColumn> </GridColumns> </SfGrid> @code {
System.Data.DataTable dtOrder;
public List<string> Columns; public List<ExpandoObject> CustomerList;
protected override void OnInitialized() {
CustomerList = CreateOrderRows();
}
public List<ExpandoObject> CreateOrderRows() { dtOrder = new DataTable("orders"); System.Data.DataColumn dcOrderID = new DataColumn("OrderID", typeof(String)); System.Data.DataColumn dcCustomerID = new DataColumn("CustomerID", typeof(String)); dtOrder.Columns.Add(dcOrderID); dtOrder.Columns.Add(dcCustomerID);
dtOrder.Rows.Add("Order1", "Customer1"); dtOrder.Rows.Add("Order1", "Customer2"); dtOrder.Rows.Add("Order2", "Customer2");
var lstRows = new List<ExpandoObject>(); foreach (System.Data.DataRow row in dtOrder.Rows) { System.Dynamic.ExpandoObject e = new System.Dynamic.ExpandoObject(); foreach (System.Data.DataColumn col in dtOrder.Columns) e.TryAdd(col.ColumnName, row.ItemArray[col.Ordinal]); lstRows.Add(e);
} return lstRows;
}
}
|
Regards,
Prathap s