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,
After further research this functionality was determined to be a new feature request. This means that this feature will be implemented and added in some of the upcomming releases. The feature request is logged with high priority and is with ID - FR13933.
Currently you can use the following approach as workaround - convert DataTable to JSON. I am attaching a sample showing how you can implement this approach.
Let me know if you have additional questions.
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.
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..
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.