@Html.EJ().Button("GridButton").Text("RenderGrid").ClientSideEvents(eve => eve.Click("GridUpdateHandler"))
<div id="Grid"></div>
<script>
function GridUpdateHandler(args) {
$.ajax({
url: '@Url.Action("CustomGridpartialView", "Grid")',
type: 'GET',
success: function (data) {
var data1 = ej.parseJSON(data);
$("#Grid").ejGrid({
columns: [
{ field: "OrderID", headerText: "Order ID" },
{ field: "CustomerID", headerText: "CustomerID", }
],
dataSource: data1,
dataBound: function (args) {
this.ignoreOnExport.splice(this.ignoreOnExport.indexOf('dataSource'), 1);
},
toolbarClick: function (args) {
this.exportGrid = this["export"];
if (args.itemName == "Excel Export") {
this.exportGrid('/Grid/ExportToExcel')
args.cancel = true;
}
},
});
},
});
}
</script>
In server side
public static List<ExpandoObject> order = new List<ExpandoObject>();
public ActionResult GridFeatures()
{
if (order.Count() == 0)
BindDataSource();
return View();
}
public ActionResult CustomGridpartialView()
{
var data = order;
return Json(data, JsonRequestBehavior.AllowGet);
}
public void BindDataSource()
{
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new ExpandoObject(code + 1, "");
order.Add(new ExpandoObject(code + 2, "ANATR");
order.Add(new ExpandoObject(code + 3, "ANTON");
order.Add(new ExpandoObject(code + 4, "BLONP");
order.Add(new ExpandoObject(code + 5, "BOLID");
code += 5;
}
}
public class ExpandoObject
{
public ExpandoObject()
{
}
public ExpandoObject(int OrderID, string CustomerId,)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
}
public int? OrderID { get; set; }
public string CustomerID { get; set; }
}
public void ExportToExcel(string GridModel)
{
ExcelExport exp = new ExcelExport();
GridProperties gridProperty = ConvertGridObject(GridModel);
List<Object> collection = new List<Object>((IEnumerable<Object>)gridProperty.DataSource);
exp.Export(gridProperty, collection, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
}
private GridProperties ConvertGridObject(string gridProperty)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
GridProperties gridProp = new GridProperties();
foreach (KeyValuePair<string, object> ds in div)
{
if (ds.Key == "dataSource")
{
string serialize = serializer.Serialize(ds.Value);
gridProp.DataSource = serializer.Deserialize<List<ExpandoObject>>(serialize);
}
else
{
var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
if (property != null)
{
Type type = property.PropertyType;
string serialize = serializer.Serialize(ds.Value);
object value = serializer.Deserialize(serialize, type);
property.SetValue(gridProp, value, null);
}
}
}
return gridProp;
}
|
|