@Html.EJS().DropDownList("Site").Change("serviceList_change").Width("150px").DataSource((IEnumerable<Object>)@ViewBag.dropdata).Render()
@Html.EJS().Grid("DataGrid").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col=> {
col.Field("OrderID").IsPrimaryKey(true).Add();
col.Field("CustomerID").Add();
col.Field("EmployeeID").Add();
}).Render()
<script>
function serviceList_change(args) {
var grid = document.getElementById('DataGrid').ej2_instances[0]; // Grid instance
var dropObjsite = document.getElementById("Site").ej2_instances[0]; // DropdownList instance
var dropdowndata = dropObjsite.value
$.ajax({
url: "/DataGrid/NewDatasource",
type: 'POST',
data: { val:dropdowndata },
success: function (response) {
grid.dataSource = response; // provide the datasource to the Grid
},
});
}
</script> |
Hi Nivas,
Greetings from Syncfusion.
We have analyzed your code and found that you want to provide the datasource to the Grid in success event event of the jquery ajax. Please refer the below code snippet to achieve your requirement,
@Html.EJS().DropDownList("Site").Change("serviceList_change").Width("150px").DataSource((IEnumerable<Object>)@ViewBag.dropdata).Render()@Html.EJS().Grid("DataGrid").DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col=> {col.Field("OrderID").IsPrimaryKey(true).Add();col.Field("CustomerID").Add();col.Field("EmployeeID").Add();}).Render()<script>function serviceList_change(args) {var grid = document.getElementById('DataGrid').ej2_instances[0]; // Grid instancevar dropObjsite = document.getElementById("Site").ej2_instances[0]; // DropdownList instancevar dropdowndata = dropObjsite.value$.ajax({url: "/DataGrid/NewDatasource",type: 'POST',data: { val:dropdowndata },success: function (response) {grid.dataSource = response; // provide the datasource to the Grid},});}</script>
In this code we have used dataSource property of the Grid to change the Grid default dataSource. We have prepared the sample with your requirement and that can be download from the below link,
Refer the below link to know about dataSource property of the Grid,
Please refer this sample for further assistance. Also we would like to let you know about our EJ2 ajax, in case if you are not aware of it. Please refer the below documentation link to know about our ajax,
We suggest you to use this ajax instead of jquery ajax to achieve your requirement.
Refer the below link to know about how to change the Grid dataSource by using our EJ2 ajax,
Regards,Madhu Sudhanan P
$.ajax({
url: "/DataGrid/NewDatasource",
type: 'POST',
data: { val:dropdowndata },
success: function (response) {
var grid = document.getElementById('DataGrid').ej2_instances[0];
grid.dataSource = response; // provide the datasource to the Grid
},
}); |
Thanks for replay, butl still not workingbelow is my code<div id="FFUpload"><a style="text-decoration:none; font-size:small">Site: </a>@Html.EJS().DropDownList("Site").PopupHeight("auto").Change("serviceList_change").Width("150px").DataSource((IEnumerable<Object>)@ViewBag.Site).Text(@ViewBag.sSite).Render()<a style="text-decoration:none; font-size:small">Customer: </a>@Html.EJS().DropDownList("Customer").PopupHeight("500px").Width("150px").Change("customerList_change").Text(@ViewBag.sCustomer).Render()<a style="text-decoration:none; font-size:small">Drop Down: </a>@Html.EJS().DropDownList("Dropdown").PopupHeight("500px").Width("200px").Text(@ViewBag.sDropdown).Render()<input type="submit" id="Insert" onclick="if (!InsertValidaton()) { return false; }" class="e-primary" value="Get Status" style="font-size:12pt;width:90px;color:white;background:none;background-color:deeppink;border: 1px solid #fff;" /><div><h2 style="font-size:small">Batch List (with comma seperator) <label style="color:red"> @ViewBag.sError</label></h2>@Html.TextArea("Query", (string)ViewBag.Query, new {@style = "width:90%;" +"height:50px;" +"max-width:90%;" +"max-height:30%;"})@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.datasource).AllowFiltering().AllowMultiSorting(true).AllowTextWrap(true).AllowPaging(true).AllowExcelExport(true).AllowResizing(true).Height("400").AllowExcelExport().ToolbarClick("toolbarClick").Toolbar(new List<string>() { "ExcelExport" }).FilterSettings(filter => { filter.Type(Syncfusion.EJ2.Grids.FilterType.Excel); }).AllowGrouping(false).Render()</div><script type="text/javascript">function serviceList_change() {var dropObj = document.getElementById("Site"); //to get dropdown list objectvar ddldist = document.getElementById('Customer').ej2_instances[0];$.ajax({type: "POST",url: "/Reports/FFUploadSelectcustomer",data: JSON.stringify({ ssite: dropObj.value }),contentType: "application/json; charset=utf-8",dataType: "json",success: function (data) {ddldist.dataSource = data;ddldist.dataBind();},error: function (response) {alert("fa");// window.location.reload();}});}</script><script type="text/javascript">function customerList_change() {var dropObj = document.getElementById("Site"); //to get dropdown list objectvar ddldist = document.getElementById('Customer').ej2_instances[0];var ddlddropdownist = document.getElementById('Dropdown').ej2_instances[0];$.ajax({type: "POST",url: "/Reports/FFUploadSelectDropdown",data: JSON.stringify({ ssite: dropObj.value, sCustomer: ddldist.value }),contentType: "application/json; charset=utf-8",dataType: "json",success: function (data) {ddlddropdownist.dataSource = data;ddlddropdownist.dataBind();},error: function (response) {alert("fa");// window.location.reload();}});}</script><script type="text/javascript">function InsertValidaton() {var dropObjsite = document.getElementById("Site"); //to get dropdown list objectvar dropObjgrid = document.getElementById("Grid");if (dropObjsite.value == null || dropObjsite.value == "") {alert("Site should not be empty");return false;}var dropObjcust = document.getElementById("Customer"); //to get dropdown list objectif (dropObjcust.value == null || dropObjcust.value == "") {alert("Customer should not be empty");return false;}// alert("test");var dropObjdropdown = document.getElementById("Dropdown"); //to get dropdown list objectif (dropObjdropdown.value == null || dropObjdropdown.value == "") {alert("Drop down should not be empty");return false;}// obtain the object reference for the <textarea>// alert("test");var txtarea = document.getElementById("Query");// obtain the index of the first selected charactervar alltext = txtarea.value;// obtain the object reference for the <textarea>var txtarea = document.getElementById("Query");// obtain the index of the first selected charactervar alltext = txtarea.value;//var start = txtarea.selectionStart;//// obtain the index of the last selected character//var finish = txtarea.selectionEnd;//// obtain the selected text//var sel = txtarea.value.substring(start, finish);//// do something with the selected content////alert(sel);if (alltext == null || alltext == "") {alert("atleast one batch needed...");return false;}$.ajax({type: "POST",url: "/Reports/FFUploadGetStatus",data: JSON.stringify({ Site: dropObjsite.value, Customer: dropObjcust.value, Dropdown: dropObjdropdown.value, Query: alltext }),contentType: "application/json; charset=utf-8",dataType: "json",success: function (data) {alert("sucess");var dropObjgrid = document.getElementById("Grid").ej2_instances[0];alert("sucess2");dropObjgrid.dataSource = data;//dropObjgrid.dataBound();alert("sucess1");},error: function (response) {alert("fa");// window.location.reload();}});return true;//window.location.reload();}</script>
@{
ViewBag.Title = "Grid localization";
}
@Html.EJS().Button("element").Content("Change Grid DataSource").Render();
<div>
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowPaging().PageSettings(page => page.PageSize(8)).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()
</div>
<script>
document.getElementById("element").addEventListener("click", function () {
var grid = document.getElementById('Grid').ej2_instances[0]; // Grid instance
$.ajax({
url: "/Home/DataSource",
type: "POST",
success: function (result) {
grid.dataSource = result;
}
})
})
</script> |
Hi Nivas,Query: Thanks for replay, butl still not working.From your code snippet, you bound the grid datasource dynamically by using external button. For your reference, we have created sample based on your requirement. Here, we have changed grid datasource dynamically by using button click. Please find the below code example and sample for your reference.[code example]
@{ViewBag.Title = "Grid localization";}@Html.EJS().Button("element").Content("Change Grid DataSource").Render();<div>@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowPaging().PageSettings(page => page.PageSize(8)).EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true).Mode(Syncfusion.EJ2.Grids.EditMode.Normal); }).Toolbar(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel" }).Render()</div><script>document.getElementById("element").addEventListener("click", function () {var grid = document.getElementById('Grid').ej2_instances[0]; // Grid instance$.ajax({url: "/Home/DataSource",type: "POST",success: function (result) {grid.dataSource = result;}})})</script>Sample link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/gridmvcDataAjax-1346378051If you are still facing the same problem, could you please bind actionFailure event to grid and share the error details if any error throws or screenshot of the grid after the data bind.Regards,Madhu Sudhanan P
Hi Nivas,
As per your given detail we suspect that you want to fit columns based on the content. So, we suggest you to use autoFitColumns method of Grid.
Refer the below documentation for further details.
Regards,Thavasianand S.