}public string StudNo { get; set; }public string School { get; set; }public string FirstName { get; set; }public string LastName { get; set; }public List<ScoreInfo> ScaleScores { get; set; }public List<ScoreInfo> Levels { get; set; }public List<ScoreInfo> RawScores { get; set; }public List<ScoreInfo> Quartile { get; set; }public List<ScoreInfo> PercentileRank { get; set; }public List<ScoreInfo> PercentileiReady { get; set; }public List<ScoreInfo> SubSkill1 { get; set; }public List<ScoreInfo> SubSkill2 { get; set; }public List<ScoreInfo> SubSkill3 { get; set; }public List<ScoreInfo> SubSkill4 { get; set; }public List<ScoreInfo> SubSkill5 { get; set; }public List<ScoreInfo> Vocabulary { get; set; }public List<ScoreInfo> Phonics { get; set; }public List<ScoreInfo> High_Frequency { get; set; }public List<ScoreInfo> Literature { get; set; }public List<ScoreInfo> Informational { get; set; }public List<ScoreInfo> Correct { get; set; }public List<ScoreInfo> MCCorrect { get; set; }public List<ScoreInfo> TEICorrect { get; set; }public List<ScoreInfo> NPR { get; set; }public List<ScoreInfo> Score { get; set; }public int SchoolYear { get; set; }public List<string> Subjects { get; set; }
public class ScoreInfo { public string SubjectName { get; set; } public int Score { get; set; } public string Schoolyear { get; set; } public string AdministrationName { get; set; } }
the Grid is being build as:
@{ ViewBag.Title = "SyncFusionListView"; Layout = "~/Views/Shared/_Layout.cshtml"; IEnumerable<StudentTestScores> dataSource = (IEnumerable<StudentTestScores>)Model; }
@section SampleHeading{<span class="sampleName"> </span>} @section ControlsSection{ @(Html.EJ().Grid<StudentTestScores>("FlatGrid") .Datasource(dataSource) .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items => { items.AddTool(ToolBarItems.ExcelExport); items.AddTool(ToolBarItems.WordExport); items.AddTool(ToolBarItems.PdfExport); items.AddTool(ToolBarItems.PrintGrid); })) .AllowSorting() .AllowPaging() .PageSettings(page => { page.PageSize(5); }) .Columns(col => { StudentTestScores studentTestScore = dataSource.First(); Type modelType = studentTestScore.GetType(); IList<PropertyInfo> props = new List<PropertyInfo>(modelType.GetProperties()); foreach (PropertyInfo prop in props) { string propName = prop.Name; var val = prop.GetValue(studentTestScore, null); if (val != null) { string header = propName; string myType = val.GetType().Name.ToString(); if (myType.ToLower().Contains("list")) // this is ScoreInfo { var scoreInfoLst = (List<ScoreInfo>)val; if (scoreInfoLst.Count > 0) { ScoreInfo scoreInfo = scoreInfoLst[0]; header = String.Format("{0} {1} {2}", scoreInfo.SubjectName, scoreInfo.AdministrationName, propName); col.Field(prop.Name).HeaderText(header).TextAlign(TextAlign.Center).Add(); } } else { if (header == "FirstName") col.Field("FirstName").HeaderText("First Name").TextAlign(TextAlign.Left).Add(); if (header == "LastName") col.Field("LastName").HeaderText("Last Name").TextAlign(TextAlign.Left).Add(); } } } })) } @Html.EJ().ScriptManager()I need to set ScoreInfo.Score need as the value for the respective column but. Can anyone give me some help on this?
If column property type is collection of array we need to traverse data with dot and index. So, we suggest you to use the below code example, to bind ScoreInfo.Score value for the respective column in Grid .
if (val != null)
{
string header = propName;
string myType = val.GetType().Name.ToString();
if (myType.ToLower().Contains("list")) // this is ScoreInfo
{
var scoreInfoLst = (List<SyncfusionMvcApplication84.Controllers.GridController.ScoreInfo>)val;
if (scoreInfoLst.Count > 0)
{
SyncfusionMvcApplication84.Controllers.GridController.ScoreInfo scoreInfo = scoreInfoLst[0];
header = String.Format("{0} {1} {2}", scoreInfo.Score, scoreInfo.AdministrationName, propName);
string Score = string.Concat(propName, ".0.","Score");
col.Field(Score).HeaderText(header).TextAlign(TextAlign.Center).Add();
}
} |
StudentTestScores studentTestScore = students.First();
Type modelType = studentTestScore.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(modelType.GetProperties());IList<object> dataSource = new List<object>();IList<string> headers = new List<string>();foreach (StudentTestScores sts in students){Dictionary<string, string> dic = new Dictionary<string, string>();foreach (PropertyInfo prop in props){string propName = prop.Name;
var val = prop.GetValue(sts, null);if (val != null && propName != "StudNo" && propName != "SchoolYear" && propName != "Data"){string header = propName;
string myType = val.GetType().Name.ToString();
if (myType.ToLower().Contains("list")) // this is ScoreInfo{var scoreInfoLst = (List<ScoreInfo>)val;if (scoreInfoLst.Count > 0)
{ScoreInfo scoreInfo = scoreInfoLst[0];
header = String.Format("{0} {1} {2}", scoreInfo.SubjectName, scoreInfo.AdministrationName, propName);dic.Add(header, scoreInfo.Score.ToString());if (!headers.Contains(header))
{headers.Add(header);}}}else
{if (header == "FirstName") header = "First Name"; //col.Field("FirstName").HeaderText("First Name").TextAlign(TextAlign.Left).Add();if (header == "LastName") header = "Last Name"; //col.Field("LastName").HeaderText("Last Name").TextAlign(TextAlign.Left).Add();dic.Add(header, val.ToString());if (!headers.Contains(header))
{headers.Add(header);}}}}dataSource.Add((object)dic);
}List<Column> cols = new List<Column>();foreach (string h in headers){cols.Add(new Column() { Field = h, HeaderText = h });}ViewBag.columns = cols;return (IEnumerable<object>)dataSource;
@(Html.EJ().Grid<StudentTestScores>("gvStudentsScores").Datasource(dataSource).ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>{items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.WordExport);
items.AddTool(ToolBarItems.PdfExport);
items.AddTool(ToolBarItems.PrintGrid);
})).AllowSorting().AllowPaging().PageSettings(page => { page.PageSize(20); }).Columns(cols))
@Html.EJ().ScriptManager()
Thank you for your response.I have implemented another method to retrieve:StudentTestScores studentTestScore = students.First();
Type modelType = studentTestScore.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(modelType.GetProperties());IList<object> dataSource = new List<object>();IList<string> headers = new List<string>();foreach (StudentTestScores sts in students){Dictionary<string, string> dic = new Dictionary<string, string>();foreach (PropertyInfo prop in props){string propName = prop.Name;
var val = prop.GetValue(sts, null);if (val != null && propName != "StudNo" && propName != "SchoolYear" && propName != "Data"){string header = propName;
string myType = val.GetType().Name.ToString();
if (myType.ToLower().Contains("list")) // this is ScoreInfo{var scoreInfoLst = (List<ScoreInfo>)val;if (scoreInfoLst.Count > 0)
{ScoreInfo scoreInfo = scoreInfoLst[0];
header = String.Format("{0} {1} {2}", scoreInfo.SubjectName, scoreInfo.AdministrationName, propName);dic.Add(header, scoreInfo.Score.ToString());if (!headers.Contains(header))
{headers.Add(header);}}}else
{if (header == "FirstName") header = "First Name"; //col.Field("FirstName").HeaderText("First Name").TextAlign(TextAlign.Left).Add();if (header == "LastName") header = "Last Name"; //col.Field("LastName").HeaderText("Last Name").TextAlign(TextAlign.Left).Add();dic.Add(header, val.ToString());if (!headers.Contains(header))
{headers.Add(header);}}}}dataSource.Add((object)dic);
}List<Column> cols = new List<Column>();foreach (string h in headers){cols.Add(new Column() { Field = h, HeaderText = h });}ViewBag.columns = cols;return (IEnumerable<object>)dataSource;And Razor:@(Html.EJ().Grid<StudentTestScores>("gvStudentsScores").Datasource(dataSource).ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>{items.AddTool(ToolBarItems.ExcelExport);
items.AddTool(ToolBarItems.WordExport);
items.AddTool(ToolBarItems.PdfExport);
items.AddTool(ToolBarItems.PrintGrid);
})).AllowSorting().AllowPaging().PageSettings(page => { page.PageSize(20); }).Columns(cols))
@Html.EJ().ScriptManager()
Now I am facing another problem:According to what user selects, other columns are to be added on postback but the Grid columns are not being updated to match the new columns set.
@{ ViewBag.Title = "Grid"; Layout = null; IEnumerable<object> dataSource = (IEnumerable<object>)Model; List<Column> cols = (List<Column>)ViewBag.columns; }
GridFeatures.cshtml
<button id="click">Refresh Column</button>
@(Html.EJ().Grid<SyncfusionMvcApplication84.Controllers.GridController.Orders>("FlatGrid")
.Datasource(dataSource)
.AllowPaging() /*Paging Enabled*/
.Columns(ViewBag.columns))
<script type="text/javascript">
$("#click").click(function () {
$.ajax({
url: "/Grid/NewColumn",
type: "POST",
datatype: "json",
contentType: "application/json",
success: function (result) {
var columns = JSON.parse(result); // deserialize the columns
var obj = $("#FlatGrid").ejGrid("instance")
obj.model.columns=[]; //if you want add to additonal columns please remove this line
obj.columns(columns); // refresh the grid columns
}
});
});
</script>GridController.cs
public ActionResult NewColumn()
{
SerializeObject serialize = new SerializeObject();
string newcol = serialize.SerializeToJson(cols.Take(4).ToList());// serialize the new coluumns columns
return Json(newcol);
} |