GridView aggregate function repeated

0 Answers 68 Views
GridView
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Deltaohm asked on 11 May 2022, 03:41 PM

Hi

I have a GridView dinamically builded.  Columns are grouped and they can have an aggregate function. Some columns has different group but same aggregate function. 
I attached a screenshot.
There are 2 columns (min26 and min27), every column has AggregateMin as function. Caption of aggregatefunction is $"AggregateMin - {col.Header} - {col.ColumnGroupName} - ".
As you can see in the screenshot the two rows reports caption for every column.
I don't understand where is the problem.

This is the code that create the grid (it is quite complex but I think that is enough to understand the structure
		private void SetTableColumns(System.Collections.ObjectModel.ObservableCollection<RiassuntoElement> elementi
			, IEnumerable<LivelliSorgente> livelli = null)
		{
			RiassuntoSorgentiGrid.Columns.Clear();
			RiassuntoSorgentiGrid.ColumnGroups.Clear();
			if (elementi.Count() > 0)
			{
				GridViewDataColumn col;
				RiassuntoSorgentiGrid.ColumnGroups.Add(new GridViewColumnGroup()
				{
					Header = "",
					Name = GROUPNAME,
				});
				col = new GridViewDataColumn()
				{
					Header = Utility.LoadString(ResourcesKeys.SorgenteColumnHeader),
					ColumnGroupName = GROUPNAME,
					DataMemberBinding = new Binding("Sorgente.Name")
					{
						Mode = BindingMode.OneWay
					}
				};
				col.Footer = Utility.LoadString(ResourcesKeys.AggregatiHeader);
				col.FooterTextAlignment = TextAlignment.Center;
				RiassuntoSorgentiGrid.Columns.Add(col);
				RiassuntoSorgentiGrid.ColumnGroups.Add(new GridViewColumnGroup()
				{
					Header = Utility.LoadString(ResourcesKeys.GeneralTabHeader),
					Name = GENERALGROUPNAME,
				});
				if (LivelliRichiesti.Contains(LivelliSorgente.Durata))
				{
					col = new GridViewDataColumn()
					{
						Header = Utility.LoadString(ResourcesKeys.DurataComplessiva),
						ColumnGroupName = GENERALGROUPNAME,
						DataMemberBinding = new Binding("Sorgente.DurataComplessiva")
						{
							Mode = BindingMode.OneWay,
							Converter = new TimeSpanToStringOver24()
						}
					};
					SetAggregateFunction(LivelliSorgente.Durata, col);
					RiassuntoSorgentiGrid.Columns.Add(col);
				}

				RiassuntoSorgentiGrid.Columns.Add(new GridViewDataColumn()
				{
					Header = Utility.LoadString(ResourcesKeys.StartLabel),
					ColumnGroupName = GENERALGROUPNAME,
					IsSortable = true,
					DataFormatString = Application.Current.FindResource(ResourcesKeys.StringFormatTime).ToString(),
					DataMemberBinding = new Binding("Sorgente.IstanteIniziale")
					{
						Mode = BindingMode.OneWay
					}
				});

				RiassuntoSorgentiGrid.Columns.Add(new GridViewDataColumn()
				{
					Header = Utility.LoadString(ResourcesKeys.EndLabel),
					ColumnGroupName = GENERALGROUPNAME,
					DataFormatString = Application.Current.FindResource(ResourcesKeys.StringFormatTime).ToString(),
					DataMemberBinding = new Binding("Sorgente.IstanteFinale")
					{
						Mode = BindingMode.OneWay
					}
				});

				if (LivelliRichiesti.Contains(LivelliSorgente.Ripetizioni))
				{ 
					RiassuntoSorgentiGrid.Columns.Add(new GridViewDataColumn()
					{
						Header = Utility.LoadString(ResourcesKeys.RipetizioniSorgente),
						ColumnGroupName = GENERALGROUPNAME,
						DataFormatString = "D",
						DataMemberBinding = new Binding("Sorgente.Ripetizioni")
						{
							Mode = BindingMode.OneWay
						}
					});
				}
				for (int i = 0; i < elementi.First().Parametri.Count; i++)
				{
					SetSorgenteColumns(elementi, i, livelli);
				}
			}
		}

		int _numCol = 0;
		private void SetSorgenteColumns(System.Collections.ObjectModel.ObservableCollection<RiassuntoElement> elementi
			, int i, IEnumerable<LivelliSorgente> livelli)
		{
			try
			{

				if (livelli == null)
					livelli = LivelliRichiesti;
				var parametro = elementi.First().Parametri[i];
				string groupName = "Group" + parametro.Id;
				var group = new GridViewColumnGroup()
				{
					Header = parametro.Name,
					Name = groupName,
				};

				RiassuntoSorgentiGrid.ColumnGroups.Add(group); 
			
				foreach (var l in livelli)
				{
					if (l == LivelliSorgente.Durata || l == LivelliSorgente.Ripetizioni)
						continue;
					var attributes = l.GetType().GetMember(l.ToString())[0].GetCustomAttributes(typeof(TypeLivelliSorgenteAttribute), false);
					if (attributes.Length > 0 && attributes[0] is TypeLivelliSorgenteAttribute typeLivello)
					{
						var col = new GridViewDataColumn()
						{
							Name = l.ToString() + _numCol,
							Header = l.ToString() + _numCol++,
							ColumnGroupName = groupName,
							DataFormatString = typeLivello.TypeLivello switch
							{
								TypeLivelliSorgente.Date => Application.Current.FindResource(ResourcesKeys.StringFormatTime).ToString(),
								TypeLivelliSorgente.Double => "F1",
								TypeLivelliSorgente.Int => "D",
								TypeLivelliSorgente.TimeSpan => "",
								_ => "F1"
							},
							DataMemberBinding = new Binding($"Sorgente.LivelliSingoli[{parametro.Id}][{l}]")
							{
								Mode = BindingMode.OneWay,
								FallbackValue = "0",
								TargetNullValue = "0",
								Converter = (typeLivello.TypeLivello == TypeLivelliSorgente.TimeSpan)
									? new TimeSpanToStringOver24()
									: null
							}
						};
						SetAggregateFunction(l, col);
						
						RiassuntoSorgentiGrid.Columns.Add(col);
					}
				}
			}
			catch (Exception ex)
			{
				throw;
			}
		}

		private void SetAggregateFunction(LivelliSorgente l, GridViewDataColumn col)
		{
			col.AggregateFunctions.Clear();
			switch (l)
			{
				case LivelliSorgente.Leq:
					col.AggregateFunctions.Add(new AggregateLeqFunction()
					{
						Caption = $"AggregateLeq - {col.Header} - {col.ColumnGroupName} - ",
						FunctionName = "AggregateLeq",
						ResultFormatString = "{0:F1}",						
					});
					break;
				case LivelliSorgente.Max:
					col.AggregateFunctions.Add(new AggregateMaxFunction()
					{
						Caption = string.Empty,
						FunctionName = "AggregateMax",
						ResultFormatString = "{0:F1}"
					});
					break;
				case LivelliSorgente.TimeMax:
					col.AggregateFunctions.Add(new AggregateTimeMaxFunction
					{
						Caption = string.Empty,
						FunctionName = "AggregateTimeMax",
					});
					break;
				case LivelliSorgente.Min:
					col.AggregateFunctions.Add(new AggregateMinFunction()
					{
						Caption = $"AggregateMin - {col.Header} - {col.ColumnGroupName} - ",
						FunctionName = "AggregateMin",
						ResultFormatString = "{0:F1}"
					});
					break;
				case LivelliSorgente.TimeMin:
					col.AggregateFunctions.Add(new AggregateTimeMinFunction
					{
						Caption = string.Empty,
						FunctionName = "AggregateTimeMin",
					}) ;
					break;
				case LivelliSorgente.Ripetizioni:
					break;
				case LivelliSorgente.Durata:
					col.AggregateFunctions.Add(new AggregateDurataFunction()
					{
						Caption = string.Empty,
						FunctionName = "AggregateDurata"
					});
					break;
				case LivelliSorgente.Sel:
					break;
				case LivelliSorgente.LeqSpalmato:
					break;
				default:
					break;
			}
			col.FooterTextAlignment = TextAlignment.Center;
		}
Thank you
Luigi
Vladimir Stoyanov
Telerik team
commented on 16 May 2022, 11:37 AM

Thank you for the shared code snippets. 

I went through them, however I was not able to spot a potential reason for the aggregate captions appearing twice in each column. That is why I created a small sample project trying to reproduce the scenario, but I was not able to do so. 

I am attaching the project I used for testing purposes for your reference. May I ask you to check it out and see how it differs from the setup on your end? You can modify it in order to replicate the scenario observed on your end and send it back. This will hopefully allow me to reproduce the scenario, investigate it and better assist you. 

I am looking forward to your reply.

Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
commented on 18 May 2022, 01:26 PM

Hi Vladimir,

Thank you for your work. My situation is quite more complex: I edited your sample in order to recreate the same situation. But I can't reproduce the problem. Anyway by comapration with the edited sample I understood that the problem was related with the setting of property "FunctionName" in aggregate function setting.
I saw that you didn't set that property and I did the same: the problem disappeared. Oddly, the edited sample never show the problem also setting that property.
Luigi   

Vladimir Stoyanov
Telerik team
commented on 23 May 2022, 08:14 AM

Hello Luigi, 

Thank you for the modified project.

The FunctionName property of the different aggregate function instances should be unique. If you don't need to explicitly set the FunctionName, I can suggest removing the setter as you have already done. Alternatively, you can also make sure that the FunctionName of each aggregate function is uniquely generated. 

I hope you find this helpful.

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Deltaohm
Top achievements
Rank 3
Bronze
Iron
Iron
Share this question
or