Hi All,
How to generate column dynamically in grid using mvc.
I have a date like this format
Key Name1| Key Name2| Key Name3| value Name1| value Name2| value Name3| value1|value2|value3|value4|value5|value6|
First six values I want to display in the first row in jqgrid
Next 6 values in next row.
Please help anyone to get like this as early as possible
Regards
Srujana
srujana thallamPosted Jan 14, 2015, 12:33 PM
Here is the solution
In cshtml page
---------------
<div id="dvJqGrid" class="dvjq">
<table id="grid_table" class="scroll">
<div id="gridpager" class="scroll" style="text-align: center;">div>
div>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '@Url.Action("GetTestData", "CrossReference")',
type: 'POST',
data: {},
success: function (result) {
var colNames = result.Json.colNames;
alert(colNames);
var colModels = result.Json.colModels;
alert(colModels);
var data = result.Json.data.options;
$("#grid_table").jqGrid({
datatype: 'jsonstring',
datastr: data,
colNames: colNames,
colModel: colModels,
jsonReader: {
root: 'rows',
repeatitems: false
},
gridview: true,
pager: $('#gridpager'),
cellEdit: true, // TRUE = turns on celledit for the grid.
cellsubmit : 'clientArray',
editurl: 'clientArray',
height: 349,
width: 968,
rowNum: 5,
rowList: [5, 10, 20, 50],
viewrecords: true
}).In('#gridpager'); //end jqgrid
},
error: function (result) {
alert("Error occured!");
}
}); //end ajax
});
script>
in the controller
-------------
public ActionResult GetTestData()
{
string[] arr1 = new string[] { "T1", "T2"};
int NumberOfElements = arr1.Length;
String[] Array = new String[NumberOfElements];
for (int i = 0; i < Array.Length; i++)
{
Array[i] = arr1[i].ToString();
}
var result = new
{
Json = new
{
colNames = arr1,
colModels = new[]
{
new {
index = "T1",
label = "T1",
name = "T1",
width = 100,
editable=true
},new {
index = "T2",
label = "T2",
name = "T2",
width = 100,
editable=true
}
},
data = new
{
options = new
{
page = "1",
total = "1",
records = "1",
rows = new[] {
new{T1=123,T2=321},
new{T1=4532,T2=934},
new{T1=4532,T2=934},
new{T1=4532,T2=934}
}
}
}
}
};
return Json(result, JsonRequestBehavior.AllowGet);
}
Pradeep ShetPosted Jan 14, 2015, 12:18 PM
This can be written with logic like seperating the | char. And creating the array as required.
srujana thallamPosted Jan 14, 2015, 4:51 AM
Thanks for your post. In my requirement I am not working on an entity data model. Just I have data in my controller which I get by running service and I want to display that in JQgrid which I mention in the above format.
Suraj SahooPosted Jan 14, 2015, 4:43 AM
This is a step by step article to get and know about the implementation of JQGrid mentioned in your Query.
Please post your queries if any.
Thanks