Hello,everyone,my data object is a class with two fields, one is UserName, the other is ProcedureStatistics which is a collection type, for a user's corresponding procedures may change dynamically. If I set the data object as the datasource of xamDataGrid, by default it will display the UserName in the parent grid and ProcedureStatistics's properties in the subgrid. But what I really want is display the UserName and ProcedureStatistics's properties in the same level. Although different user have different procedures, I want to display all the procedures and set the corresponding value for each user. Can anyone give me some hints ?
Thank you very much~
Steve, Thank you very much for all your help~ I'm just combing back from my 3 day's travel and it's really a wonderful feeling to see your smart answer, Thank you again~
Yeah, you are correct. The data will come out like that. With that being said, i do not think that there is an easy way with LINQ and anonymous types to do this. we may have to create a datatable based on the information because we can create columns dynamically for procedure1 procedure2, prodecureN. With anonymous types, I would have to give the members names and plus LINQ is not able to do this type of flattening of the data.
I think we will have to do this all manually. Here is a code snippet of iterating each procedureStatistic and creating columns dynamically based on procedure name.
ObservableCollection<ProductionStatistics> TotalProductionStatistics = GetItems();
////make table///// DataTable StatsTable = new DataTable("TotalProductionStatistics"); StatsTable.Columns.Add("UserName", typeof(string)); foreach (ProductionStatistics ps in TotalProductionStatistics) { //int psIndex = TotalProductionStatistics.IndexOf(ps); DataRow row = StatsTable.NewRow(); row["UserName"] = ps.UserName; foreach (ProcedureLog log in ps.ProcedureLogs) { if (StatsTable.Columns.Contains(log.Name)) { row[log.Name] = log.Count; } else { StatsTable.Columns.Add(log.Name, typeof(string)); row[log.Name] = log.Count; } } StatsTable.Rows.Add(row); }
This will produce a grid looking like the attached image.
I hope this works.
Follow this code, I' m afraid I will get the result display as the following, do I ? This is a bit different from what I expected...
Try this code
var GridList = (from s in TotalProductionStatistics select s.ProcedureLogs.Select(log => new { Username = s.UserName, ProcedureName = log.Name, Count = log.Count })).SelectMany(t => t); this.xamDataGrid1.DataSource = GridList;
This should flatten it out the way that you want
Steve, thank you for your nice suggestion, but I'm afraid I still can not solve the problem. Here I state my question in more detail and am longing for your more suggestions. Now the data binding in xml looks like DataSource="{Binding TotalProductionStatistics, Mode=TwoWay}",here the type of TotalProductionStatistics is ObservableCollection<ProductionStatistics> and
ProductionStatistics class consists two fields: string username; ObservableCollection<ProcedureLog> procedureLogs;
ProcedureLog class consists two fields: string procedureName; int count;
Binding data like this, the data will display as
but what I want to display looks like the following
The number of procedures will change dynamically, that is there maybe Procedure5 one day and Procedure6 another day. I can get all the procedures from another data structure, but the only data source of the column cells come from TotalProductionStatistics.
How should I organize my data or how to deal with the data binding to implenment the expected view? Maybe I don't fully understand you first suggestion, but I don't konw how to use that method to solve my problem. Hope you can give me more hints~