public ActionResult Index(){
List<ExpandoObject> datasource = new List<ExpandoObject>();
dynamic row = new ExpandoObject();
row.id = 1;
row.name = "Name 1";
dynamic row1 = new ExpandoObject();
row1.id = 2;
row1.name = "Name 2";
var dt = new DataTable();
foreach (var val2 in ((IDictionary<string, object>)datasource[0]).Keys)
{
dt.Columns.Add(val2);
}
foreach (dynamic val in datasource)
{
IDictionary<string, object> items = val;
dt.Rows.Add(items.Values.ToArray());
}
ViewBag.datasource = dt;
return View();
}
@(Html.EJ().Grid<object>("Grid")
.Datasource((IEnumerable<object>)ViewBag.datasource)
.AllowPaging()
.PageSettings(settings => settings.PageSize(10))
.AllowFiltering()
.AllowSorting()
.FilterSettings(d => d.FilterType(FilterType.Menu))
.EnablePersistence(false)
//columns not declared
) |
Thank you very much! - I got the idea, but have to sligtly modify your source snippet as it was throwing some cast exceptions. Here is final piece for other people who might find this useful:
public ActionResult AppGrid(int id)
{
List<ExpandoObject> datasource = new List<ExpandoObject>();
dynamic row = new ExpandoObject();
row.id = 1;
row.name = "Name 1";
datasource.Add(row as ExpandoObject);
dynamic row1 = new ExpandoObject();
row1.id = 2;
row1.name = "Name 2";
datasource.Add(row1 as ExpandoObject);
var dt = new System.Data.DataTable();
foreach (var val2 in ((IDictionary<string, object>)datasource[0]).Keys)
{
dt.Columns.Add(val2);
}
foreach (dynamic val in datasource)
{
IDictionary<string, object> items = val;
dt.Rows.Add(items.Values.ToArray());
}
ViewBag.datasource = dt;
return View("DynamicAppGrid");
}
private IEnumerable<ExpandoObject> Add(IEnumerable<ExpandoObject> e, ExpandoObject value)
{
foreach (var cur in e)
{
yield return cur;
}
yield return value;
}
@(Html.EJ().Grid<object>("Grid")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
.PageSettings(settings => settings.PageSize(10))
.AllowFiltering()
.AllowSorting()
.FilterSettings(d => d.FilterType(FilterType.Menu))
.EnablePersistence(false)
//columns not declared
}
@(Html.EJ().Grid<object>("BatchEditing")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
.PageSettings(settings => settings.PageSize(10))
.AllowFiltering()
.AllowSorting()
.FilterSettings(d => d.FilterType(FilterType.Menu))
.EnablePersistence(false)
.ClientSideEvents(events => events.Load("onLoad"))
//columns not declared
)
<script>
function onLoad(args) {
var jsonObj = this.model.dataSource[0];
if (this.model.columns.length == 0 && jsonObj) {
for (var field in jsonObj) {
var value = jsonObj[field];
this.model.columns.push({
field: field,
width: 50,
headerText: field,
textAlign: ej.TextAlign.Right
});
}
}
}
</script> |
@(Html.EJ().Grid<object>("BatchEditing")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
. . .
. .
.ClientSideEvents(events => events.Load("onLoad"))
//columns not declared
)
<script>
function onLoad(args) {
var jsonObj = this.model.dataSource[0];
if (this.model.columns.length == 0 && jsonObj) {
for (var field in jsonObj) {
if (jsonObj.hasOwnProperty(field)) {
var value = jsonObj[field];
this.model.columns.push({
field: field,
width: 50,
type: value != null ? (value.getDay ? (value.getHours() > 0 || value.getMinutes() > 0 || value.getSeconds() > 0 || value.getMilliseconds() > 0 ? "datetime" : "date") : typeof (value)) : null,
headerText: field,
textAlign: ej.TextAlign.Right
});
}
}
}
}
</script>
public ActionResult Index(){
List<ExpandoObject> datasource = new List<ExpandoObject>();
dynamic row = new ExpandoObject();
row.id = 1;
row.verified = true;
row.name = "Name 1";
datasource.Add(row);
dynamic row1 = new ExpandoObject();
row1.id = 2;
row1.verified = false;
row1.name = "Name 2";
datasource.Add(row1);
var dt = new DataTable();
foreach (var col in ((IDictionary<string, object>)datasource[0]))
{
dt.Columns.Add(col.Key, col.Value.GetType());//Columns and type
}
foreach (dynamic val in datasource)
{
IDictionary<string, object> items = val;
dt.Rows.Add(items.Values.ToArray());
}
ViewBag.datasource = dt;
return View();
} |
@(Html.EJ().Grid<object>("BatchEditing")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
.ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
//columns not declared
)
<script>
function onLoad(args) {
var jsonObj = this.model.dataSource[0];
if (this.model.columns.length == 0 && jsonObj) {
for (var field in jsonObj) {
var value = jsonObj[field];
//from the object values type has been defined
//so you can dynamically assign them to the columns
type = value != null ? (value.getDay ? (value.getHours() > 0 || value.getMinutes() > 0 || value.getSeconds() > 0 || value.getMilliseconds() > 0 ? "datetime" : "date") : typeof (value)) : null;
this.model.columns.push({
field: field,
width: 50,
type: type,
headerText: field,
textAlign: ej.TextAlign.Right
});
}
}
}
</script> |
@(Html.EJ().Grid<object>("BatchEditing")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
.EditSettings(edit => edit.AllowAdding().AllowDeleting().AllowEditing())
.ToolbarSettings(tools => tools
.ShowToolbar()
.ToolbarItems(items =>
{
items.AddTool(ToolBarItems.Add);
items.AddTool(ToolBarItems.Edit);
items.AddTool(ToolBarItems.Update);
items.AddTool(ToolBarItems.Delete);
items.AddTool(ToolBarItems.Cancel);
}
))
.ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
//columns not declared
)
<script>
function dataBound(args) {
//for editing actions, primaryKey column is must
//we have defined them in the dataBound event
//always recommended to placed the primarykey column as first column
var column = args.model.columns[0];
column.isPrimaryKey = true;
//Here columns method used to update the particular column
this.columns(column, "update");
}
</script> |
function onLoad(args) {
var jsonObj = this.model.dataSource[0];
if (this.model.columns.length == 0 && jsonObj) {
for (var field in jsonObj) {
var value = jsonObj[field];
//from the object values type has been defined
//so you can dynamically assign them to the columns
type = value != null ? (value.getDay ? (value.getHours() > 0 || value.getMinutes() > 0 || value.getSeconds() > 0 || value.getMilliseconds() > 0 ? "datetime" : "date") : typeof (value)) : null;
if (type == "boolean")
editType = "booleanedit";
else if (type == "date")
editType = "datepicker";
else if (type == "datetimepicker")
editType = "datetimepicker";
else if (type == "number")
editType = "numericedit";
else
editType = "string";
this.model.columns.push({
field: field,
width: 50,
type: type,
editType: editType,
headerText: field,
textAlign: ej.TextAlign.Right
});
}
}
}
|
@(Html.EJ().Grid<object>("BatchEditing")
.Datasource((System.Data.DataTable)ViewBag.datasource)
.AllowPaging()
.. .
.ClientSideEvents(events => events.Load("onLoad").DataBound("dataBound"))
//columns not declared
)
<script>
function onLoad(args) {
var jsonObj = this.model.dataSource[0];
if (this.model.columns.length == 0 && jsonObj) {
for (var field in jsonObj) {
var value = jsonObj[field];
//from the object values type has been defined
//so you can dynamically assign them to the columns
if (typeof (jsonObj[field]) != "object") {
type = value != null ? (value.getDay ? (value.getHours() > 0 || value.getMinutes() > 0 || value.getSeconds() > 0 || value.getMilliseconds() > 0 ? "datetime" : "date") : typeof (value)) : null;
editType = typeBuilder(type);
this.model.columns.push({
field: field,
width: 50,
type: type,
editType: editType,
headerText: field,
textAlign: ej.TextAlign.Right
});
}
else {
//for complex columns
complexColumn(jsonObj[field], field, this.model.columns);
}
}
}
}
function complexColumn(obj, field, cols, cxField) {
var complexField = cxField || field;
for (var field1 in obj) {
if (typeof obj[field1] == "object" && !ej.isNullOrUndefined(obj[field1])) {
complexField = complexField.concat(".").concat(field1);
complexColumn(obj[field1], field1, cols, complexField);
}
else {
var cxFieldName = (complexField).concat(".").concat(field1), value = obj[field1];
var editType = typeBuilder(type);
cols.push({
field: cxFieldName,
editType: editType,
type: value != null ? (value.getDay ? (value.getHours() > 0 || value.getMinutes() > 0 || value.getSeconds() > 0 || value.getMilliseconds() > 0 ? "datetime" : "date") : typeof (value)) : null
});
}
}
}
function typeBuilder(type) {
var editType = "";
if (type == "boolean")
editType = "booleanedit";
. .
else
editType = "string";
return editType;
}
|