Hello,
I want to display C# DataTable Data on igGrid But not display plz any one can help
Code:
public JsonResult BindGridVirtualization() { MMDataModelDataContext _db = new MMDataModelDataContext(_getString.GetSQLString()); GridVirtualizationModel model = new GridVirtualizationModel(); this.InitializeGridVirtualization(model.GridVirtualization);
DataTable table = GetMealTable(); var value = from c in table.AsEnumerable().AsQueryable() select c; model.GridVirtualization.DataSource = value; return model.GridVirtualization.GetData(); }
private DataTable GetMealTable()
{
DataTable tbl = new DataTable();
foreach (string str in columns) { tbl.Columns.Add(str,typeof(string)); }
var value = from c in _db.GetTable<tblMeal>() //where Convert.ToDateTime(c.Date).ToString("dd/MM/yyyy").Contains(con) where c.Date.Contains(con) orderby c.Date ascending
select c;
DataRow tr = tbl.NewRow();
foreach (tblMeal ob in value)
{ tr = tbl.NewRow();
tr[ob.MemberNickName] = ob.Amount;
tr["Date"] = ob.Date;tbl.Rows.Add(tr);
}
return tbl;
Hello jahangir_cse,
In order to use “igGrid” with “DataTable” as Data Source you should convert the “DataTable” records to “IQueryable” first.
For example if you have the following “Model” called “Person” that matches the “DataTable” structure:
public class Person { public string PersonId { get; set; } public string Gender { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } }
public class Person
public string PersonId { get; set; }
public string Gender { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
And your “View” for the grid is as follows:
@( Html.Infragistics().Grid<Person>() .AutoGenerateColumns(false) .Columns(column => { column.For(x => x.PersonId).DataType("string").HeaderText("Person ID"); column.For(x => x.FirstName).DataType("string").HeaderText("First Name"); column.For(x => x.LastName).DataType("string").HeaderText("Last Name"); column.For(x => x.Gender).DataType("string").HeaderText("Gender"); column.For(x => x.DateOfBirth).DataType("datetime").HeaderText("Date Of Birth"); }) .Features(feature => { feature.Paging().PageSize(10).PrevPageLabelText("Previous").NextPageLabelText("Next"); feature.Sorting().Mode(SortingMode.Single).ColumnSettings(settings => { settings.ColumnSetting().ColumnKey("PersonId").AllowSorting(true); }); feature.Selection().MouseDragSelect(true).MultipleSelection(true).Mode(SelectionMode.Row); }) .DataSourceUrl(Url.Action("GetPeople")) .Width("100%") .Height("350px") .DataBind() .Render() )
@( Html.Infragistics().Grid<Person>()
.AutoGenerateColumns(false)
.Columns(column =>
column.For(x => x.PersonId).DataType("string").HeaderText("Person ID");
column.For(x => x.FirstName).DataType("string").HeaderText("First Name");
column.For(x => x.LastName).DataType("string").HeaderText("Last Name");
column.For(x => x.Gender).DataType("string").HeaderText("Gender");
column.For(x => x.DateOfBirth).DataType("datetime").HeaderText("Date Of Birth");
})
.Features(feature =>
feature.Paging().PageSize(10).PrevPageLabelText("Previous").NextPageLabelText("Next");
feature.Sorting().Mode(SortingMode.Single).ColumnSettings(settings =>
settings.ColumnSetting().ColumnKey("PersonId").AllowSorting(true);
});
feature.Selection().MouseDragSelect(true).MultipleSelection(true).Mode(SelectionMode.Row);
.DataSourceUrl(Url.Action("GetPeople"))
.Width("100%")
.Height("350px")
.DataBind()
.Render()
)
Then the data needed for the grid should be converted like this from the “Controller”:
[GridDataSourceAction] public ActionResult GetPeople() { Repository.Repo repo = new Repository.Repo(); DataTable table = repo.GetDataTable(); List<Person> personColection = new List<Person>(); foreach (DataRow row in table.Rows) { Person person = new Person { PersonId = Convert.ToString(row["PersonId"]), Gender = Convert.ToString(row["Gender"]), FirstName = Convert.ToString(row["FirstName"]), LastName = Convert.ToString(row["LastName"]), DateOfBirth = Convert.ToDateTime(row["DateOfBirth"]), }; personColection.Add(person); } IQueryable<Person> people = personColection.AsQueryable<Person>(); return View(people); }
[GridDataSourceAction]
public ActionResult GetPeople()
Repository.Repo repo = new Repository.Repo();
DataTable table = repo.GetDataTable();
List<Person> personColection = new List<Person>();
foreach (DataRow row in table.Rows)
Person person = new Person
PersonId = Convert.ToString(row["PersonId"]),
Gender = Convert.ToString(row["Gender"]),
FirstName = Convert.ToString(row["FirstName"]),
LastName = Convert.ToString(row["LastName"]),
DateOfBirth = Convert.ToDateTime(row["DateOfBirth"]),
};
personColection.Add(person);
IQueryable<Person> people = personColection.AsQueryable<Person>();
return View(people);
In this way the data from the “DataTable” will be properly sent and displayed from the grid.
Test this approach and let me know what the results are on your setup.
Hi,Alex
Thanks for your reply.
You was take a model class 'Person' have fixed number of properties and grid column created by of this model properties.
But i create a grid at run time in controller with columns comes from database using GridModel.
In the controller i created a DataTable with these columns name and add DataRow in this table.
When i assign this table to the igGrid but data not display.
Controller:
DataTable table = GetMealTable(); var value = from c in table.AsEnumerable().AsQueryable() select c;model.GridVirtualization.DataSource = value;
Plz replay.
Thank you..
We will research further if binding of the grid in this way is possible without creation of a Model first.
Another possibility is to consider sending the data to the grid as JSON by converting the data from DataTable to JSON and bind the grid in that way.
I will inform you when more data are available.
Hello all,
You could find detailed information about Binding to DataTable in our official online documentation at:http://help.infragistics.com/Help/NetAdvantage/jQuery/2012.2/CLR4.0/html/igGrid_Binding_to_DataTable.html
Or take a look at the online samples: DataTable Interactions.
Alex,
Can you describe the feature or provide a link to an example? I can't find anything in the website regarding FR13933 and the ability to bind to a DataTable would be very helpful.
Thanks,
Paul
This feature is already available in versions 12.2 and later. It is now officially supported.
When FR13933 will be released and where i can see a progress?Also, if using your json sample, if first row contain Null in any of column, grid is not rendering that column.Is this a bug?
Hello Alex,
Thanks for reply with attachment.
Problem solved and have another query for igGrid.
When i edit ,insert and delete operation in this dynamic column type grid UpdateUrl Controller Action Received List of Transaction wit Generic type as Json Data format and parsing some who and Insert,Update and Delete operation is success.
Code Line:
List<Transaction<dynamic>> transactions = m.LoadTransactions<dynamic>(HttpContext.Request.Form["ig_transactions"]);
But i want to receive List of model object.
If i create a grid with Model have fixed number of properties can get List Of model object that means Json Data parsing Automatically With model class Properties and Value get by property Name.
List<Transaction<Person>> transactions = m.LoadTransactions<dynamic>(HttpContext.Request.Form["ig_transactions"]);
Person Class Have Fixed Number properties.
But I create a class with dynamic properties.
public class PersonList : DynamicObject { // The inner dictionary. Dictionary<string, object> properties = new Dictionary<string, object>(); public object this[string key] { get { if (properties.ContainsKey(key)) { return properties[key]; } return null; } set { properties[key] = value; } } public int Count { get { return properties.Count; } } public override bool TryGetMember(GetMemberBinder binder, out object result) { string name = binder.Name; return properties.TryGetValue(name, out result); } public override bool TrySetMember(SetMemberBinder binder, object value) { properties[binder.Name] = value; return true; } }
List<Transaction<PersonList >> transactions = m.LoadTransactions<dynamic>(HttpContext.Request.Form["ig_transactions"]);
I can not get Data by class property name an error fached Null reference Exception.
Plz Help and reply.
Thank You