Hi,
is it possible to tell me how can to use hierarchicalgrid using entities.
in my project i am using the simple grid..
but in those grids i have got duplicate information..
I was trying to change the grid to hierarchicalgrid...
lets say that i have a table Clients, and a table Process.
A client can have multiple process..
The client is discribed by a name, and the process is discribed by the hour of creation...
how can i create a hierarchicalgrid? in order that the main grid got the client name, and the subgrid got the process?
in the current project i have created a viewmodel containing the information that i need.
here is the code..
viewmodel class
public class VMProcessoCliente{[Key]public int IDProcesso { get; set; }public string NomeCliente { get; set; }public string DataInserido { get; set; }public string Estado { get; set; }
public static IQueryable<VMProcessoCliente> GetListaProcessosClientes(){MvcIdonic db = new MvcIdonic();
var processos = (from p in db.Processosjoin c in db.Clientes on p.IDCliente equals c.IDClienteorderby p.IDProcesso descendingwhere p.Rem == 0select new { IDProcesso = p.IDProcesso, NomeCliente = c.Nome, DataInserido = p.DataInserido, Estado = p.Estado }).ToList().Select(c => new VMProcessoCliente{IDProcesso = c.IDProcesso,NomeCliente = c.NomeCliente,DataInserido = c.DataInserido.ToString(),Estado = GetValEstado(c.Estado)});
return processos.AsQueryable<VMProcessoCliente>();
}
private static string GetValEstado(int p){if (p == 1)return "Start";elsereturn "close";}
my index.cshtm containig the grid
@( Html.Infragistics().Grid<Licenciamento_v2.Areas.Idonic.ViewModel.VMProcessoCliente>().Caption("Processo").ID("grid_Processo").DefaultColumnWidth("200px").PrimaryKey("IDProcesso").Columns(column =>{
column.For(x => x.NomeCliente).DataType("string").HeaderText("Nome do Cliente");column.For(x => x.DataInserido).DataType("DateTime").HeaderText("Data de Criação do Processo");column.For(x => x.Estado).HeaderText("Estado");column.For(x => x.IDProcesso).Width("0px");
}).Features(features =>{features.Paging().PageSize(20).PrevPageLabelText("Previous").NextPageLabelText("NEXT");features.Sorting().Mode(SortingMode.Single);features.Selection().MultipleSelection(false);features.Filtering().Mode(FilterMode.Simple);features.Updating().EnableAddRow(false).EnableDeleteRow(true).EditMode(GridEditMode.None);
}).DataSourceUrl(Url.Action("ListarProcessos")).UpdateUrl(Url.Action("DeleteProcessos")).Render())
and here is the controller code to list all the information
[GridDataSourceAction]public ActionResult ListarProcessos(){return View(Licenciamento_v2.Areas.Idonic.ViewModel.VMProcessoCliente.GetListaProcessosClientes());}
this gives me a grid with information about the process date of creation, the client of the process..
the problem is that the client can contain multiple process..
so i can ihave multiple rows with the same client name..
what i want is to have a single row per client, and in the subgrid have the info about the process...
how can i do this?
thanks in advance..
Hello Hugo,
To create a hierarchical grid you should specify the child layout(s) as the hierarchy is not inferred from the model. A sample of a hierarchical grid created in the view with chaining, similar to your case, can be found here: http://igniteui.com/hierarchical-grid/editing-dataset.
Please, let me know if you have any other questions or need additional help!
Best regards,
Stamen Stoychev
i manage to get it working last night...~
the processo class
public class Processo { [Key] public int IDProcesso { get; set; } public int IDCliente { get; set; } public DateTime DataInserido { get; set; } public int Estado { get; set; } public int IDU { get; set; } public int Rem { get; set; } public DateTime TStamp { get; set; }
the view model Client
public class Hierarchicalgrid {
public int IDCliente { get; set; } public string NomeCliente { get; set; }
public List<Processo> Processos { get; set; }
public static IQueryable<Hierarchicalgrid> GetListProcessos() { MvcIdonic db = new MvcIdonic();
var processos = (from c in db.Clientes //join p in db.Processos on c.IDCliente equals p.IDCliente orderby c.Nome descending where c.Rem == 0 select new { IDCliente = c.IDCliente, NomeCliente = c.Nome }).ToList().Select(c => new Hierarchicalgrid { IDCliente = c.IDCliente, NomeCliente = c.NomeCliente, Processos=GetListProcessosByIDCliente(c.IDCliente) });
return processos.AsQueryable<Hierarchicalgrid>();
private static List<Processo> GetListProcessosByIDCliente(int p) { MvcIdonic db = new MvcIdonic(); List<Processo> ListP = new List<Processo>(); ListP = db.Processos.Where(x => x.IDCliente == p).ToList(); return ListP; } }
the view.cshtml
@(Html.Infragistics().Grid <Licenciamento_v2.Areas.Idonic.ViewModels.Hierarchicalgrid>()
.AutoGenerateColumns(false) .AutoGenerateLayouts(false) .Caption("Processo") .ID("grid_Processo") .Columns( cols=>{ cols.For(x => x.IDCliente).HeaderText("ID"); cols.For(x => x.NomeCliente).HeaderText("Name"); }) .ColumnLayouts( layout=>{ layout.For(x => x.Processos).Columns(cols=>{ cols.For(x => x.IDProcesso).HeaderText("ID"); cols.For(x => x.DataInserido).HeaderText("Name"); cols.For(x => x.IDCliente).HeaderText("CustomerID"); }); }) .DataSourceUrl(Url.Action("ListarProcessos2")) .Width("400px") .DataBind() .Render() )
the controller
[GridDataSourceAction] public ActionResult ListarProcessos2() { return View(Licenciamento_v2.Areas.Idonic.ViewModels.Hierarchicalgrid.GetListProcessos()); }
Now how can i transform this code to load on demand...
i am currently creating a similar sample project wit companys and user...
but i want to add the load on demand part..
How can i do that...
thanks
So i manage to get it working...
my controller for now
public class HomeController : Controller { // // GET: /Idonic/Home/ public ActionResult Index() { GridModel grid = CreateGridModel(); return View(grid); } // Take grid creation outside as it will be used in all methods: private GridModel CreateGridModel() { GridModel grid = new GridModel(); // Set up properties and columns: grid.AutoGenerateColumns = false; grid.Columns = new List(); grid.Columns.Add(new GridColumn("IDEmpresa", "IDEmpresa", "number", "10px")); grid.Columns.Add(new GridColumn("Company Name", "NomeEmpresa", "string", "150px")); grid.PrimaryKey = "IDEmpresa"; grid.AutoGenerateLayouts = false; // Create child layout GridColumnLayoutModel layout = new GridColumnLayoutModel(); layout.Key = "Utilizadores"; layout.PrimaryKey = "UserId"; layout.AutoGenerateColumns = false; layout.Columns = new List(); layout.Columns.Add(new GridColumn("UserId", "UserId", "number", "100px")); //layout.Columns.Add(new GridColumn("Customer", "CustomerID", "string", "100px")); // Add paging to child layouts (different from the parent's) GridPaging layoutPaging = new GridPaging(); layoutPaging.VisiblePageCount = 2; layoutPaging.PageSize = 5; layoutPaging.Type = OpType.Remote; layout.Features.Add(layoutPaging); //add that layout to the grid grid.ColumnLayouts.Add(layout); //most importantly turn Load On demand on and define response Urls: grid.LoadOnDemand = true; grid.DataSourceUrl = this.Url.Action("BindParent"); grid.ColumnLayouts[0].DataSourceUrl = this.Url.Action("BindChild"); //Add features: GridSorting sorting = new GridSorting(); sorting.Type = OpType.Remote; grid.Features.Add(sorting); GridPaging paging = new GridPaging(); paging.PageSize = 10; paging.Type = OpType.Remote; grid.Features.Add(paging); GridFiltering filtering = new GridFiltering(); filtering.Type = OpType.Remote; grid.Features.Add(filtering); //And lastly set ID so the client widget can be normally interacted with (see the events in the view) grid.ID = "Grid_Utilizadores_By_Empresa"; // Return the finished model return grid; } public JsonResult BindParent() { //create a model GridModel grid = CreateGridModel(); //set its datasource MvcIdonic DB = new MvcIdonic(); var dataSource = DB.Empresas; grid.DataSource = dataSource.Where(x => x.Rem == 0).OrderBy(x=>x.IDEmpresa); --->linq to entites, fetch all companyes. //and use the GetData() method to return the results return grid.GetData(); } public JsonResult BindChild(string path, string layout) { string[] arrSplitVarPath = path.Split(':'); ->to find the id of the company...dont know if it is the correct way,. int IDEmpresa = Int32.Parse(arrSplitVarPath[1]); //create a model GridModel grid = CreateGridModel(); //set its datasource MvcIdonic DB = new MvcIdonic(); var dataSource = DB.UserProfiles; grid.DataSource = dataSource.Where(x => x.IdEmpresa==IDEmpresa).OrderBy(x=>x.UserName); -->fetch all users of a company. //and use the GetData() method to return the results return grid.GetData(path, layout); } }
my class Company
public class Company { [Key] public int IDEmpresa { get; set; } public string NomeEmpresa { get; set; } public string NIF { get; set; } public string Observacoes { get; set; } public int IDP { get; set; } public int IDU { get; set; } public DateTime TStamp { get; set; } public int Rem { get; set; } public String TipoCliente { get; set;} }
and for the user i am using the simplemenbership class that were created..
the user contain the username, password, name and some other fields..
then i have user profile table, this table tell me wich users are related with which company.
so a company can have multiple employees(Utilizadores).
in order to find the id of company i do the split of the string path...but i think this is wrong...because on our sample you use foreign key...and in mine i dont.
by the method i discribed the problem lays, i think ,if the grid has more thant 2 levels...
i found this way of doing things and it is working..
but if i followed this way wouldnt bring more problems along the way..
Or is there a better way?
Thanks...
Unfortunately, load on demand is not supported when the grid is initialized in the View. You'll need to set its properties in the Controller or the Model. Please refer to this help topic for details on how to configure load on demand in MVC. You can also check this sample which demonstrates it.
Please, let me know if you have any other questions!