One way to do this would be to use a linq expression to select all the Labels of the form, and Enumerable.Zip
iterate on the list of Labels and the outcome of select
of DataTable
:
// Simulando os dados do teu DataTable
DataTable tabela = new DataTable("Jogadores");
tabela.Columns.Add(new DataColumn("Nome", typeof(string)));
tabela.Columns.Add(new DataColumn("Idade", typeof(int)));
tabela.Columns.Add(new DataColumn("Sexo", typeof(char)));
tabela.Rows.Add("Maria", 20, 'f');
tabela.Rows.Add("Leticia", 25, 'f');
tabela.Rows.Add("Pedro", 30, 'm');
tabela.Rows.Add("Tiago", 40, 'm');
tabela.Rows.Add("Joao", 29, 'm');
// Selecionar linhas onde o campo idade seja maior ou igual a 29
DataRow[] resultados = tabela.Select("Idade >= 29");
// No caso do AP ele usou this.Controls["PageHeader"].AllControls<XRLabel>()
var labels = this.Controls.OfType<XRLabel>();
foreach (var tupla in labels.Zip(resultados, Tuple.Create)){
XRLabel label = tupla.Item1 as XRLabel;
string valor = tupla.Item2.Field<String>(0); // Valor do campo "Nome"
label.Text = valor;
}
No, there are over 100 Labels that I need to change, it is impossible to do in hand.
– Daniel
What class of Labels?
– Leonel Sanches da Silva
Class Xrlabel. It’s a label of the report Devexpress.XtraReports.UI.Xtrareport. I need to do in all, is a very specific report where I have no way to use a grid with Datadource because they are very different data and so I have to set individually.
– Daniel
In the php this kind of thing is very easy to do. Call a function with variable name for example:
$variable = "my_function"; MyClass->$variable();
. But here at C# I’m not getting it.– Daniel