<ej:Grid ID="FlatGrid" runat="server" ……..">
………………..
</ej:Grid>
using System;
using System.Reflection;
……………
public partial class GridFeatures : System.Web.UI.Page
{
public static List<cols> data = new List<cols>();
public static List<Orders> order = new List<Orders>();
protected void Page_Load(object sender, EventArgs e)
{
BindDataSource();
}
private void BindDataSource()
{
……………………
this.FlatGrid.DataSource = order;
this.FlatGrid.DataBind();
PropertyFinder<Orders> PropertyFinderobj = new PropertyFinder<Orders>();
PropertyFinderobj.ModelNameAndValue(order[0]);
for (int j = 0; j < data.Count(); j++)
{
string[] formats = { "M/d/yyyy h:mm:ss tt", "M/d/yyyy h:mm tt", "MM/dd/yyyy hh:mm:ss", "M/d/yyyy h:mm:ss", "M/d/yyyy hh:mm tt", "M/d/yyyy hh tt", "M/d/yyyy h:mm", "M/d/yyyy h:mm", "MM/dd/yyyy hh:mm", "M/dd/yyyy hh:mm" };
string value = data[j].columnValue;
DateTime datetime;
if (DateTime.TryParseExact(value, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.NoCurrentDateDefault, out datetime))
{
this.FlatGrid.Columns.Add(new Column() { Field = data[j].columnName, Format = "{0:dd/MM/yyyy}" }); // apply the format for date column alone
}
else
this.FlatGrid.Columns.Add(new Column() { Field = data[j].columnName });
}
}
public class PropertyFinder<Model> where Model : class
{
public void ModelNameAndValue(Model modelObj)
{
//Getting Type of Orders Class Model
Type ModelType = modelObj.GetType();
//We will be defining a PropertyInfo Object which contains details about the class property
PropertyInfo[] arrayPropertyInfos = ModelType.GetProperties();
//Now we will loop in all properties one by one to get value
foreach (PropertyInfo property in arrayPropertyInfos)
{
data.Add(new cols(property.Name, property.GetValue(modelObj).ToString()));
}
}
}
[Serializable]
public class Orders
{
. ……………..
}
[Serializable]
public class cols
{
public cols()
{
}
public cols(string columnName, string columnValue)
{
this.columnName = columnName;
this.columnValue = columnValue;
}
public string columnName { get; set; }
public string columnValue { get; set; }
}
}
}
|