<script type="text/javascript">
$(function () {
$("#Grid").ejGrid({
dataSource: ej.DataManager({
url: "/Grid/DataSource",
updateUrl: "/Grid/Update",
insertUrl: "/Grid/Insert",
removeUrl: "/Grid/Delete",
adaptor:"UrlAdaptor"
}),
..
});
});
</script>
…………………………
public ActionResult DataSource(DataManager dm)
{
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());
}
foreach (dynamic val in datasource)
{
IDictionary<string, object> items = val;
dt.Rows.Add(items.Values.ToArray());
}
IEnumerable DataSource = ConvertTableToList(dt);
DataResult result = new DataResult();
DataOperations operation = new DataOperations();
result.result = DataSource;
result.count = result.result.AsQueryable().Count();
if (dm.Skip > 0)
result.result = operation.PerformSkip(result.result, dm.Skip);
if (dm.Take > 0)
result.result = operation.PerformTake(result.result, dm.Take);
return Json(result, JsonRequestBehavior.AllowGet);
}
public static List<Dictionary<string, object>> ConvertTableToList(DataTable dt)
{
return dt.AsEnumerable()
.Select(r => r.Table.Columns.Cast<DataColumn>()
.Select(c => new KeyValuePair<string, object>(c.ColumnName, r[c.Ordinal])
).ToDictionary(z => z.Key, z => z.Value)
).ToList();
}
public ActionResult Insert(ExpandoObject value)
{
//do your operations here
}
public ActionResult Update(ExpandoObject value)
{
//do your operations here
}
public ActionResult Delete(int key)
{
//do your operations here
}
public class DataResult
{
public IEnumerable result { get; set; }
public int count { get; set; }
}
} |
<script type="text/javascript">
$(function () {
$("#Grid").ejGrid({
..
dataBound: function(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> |